整理一下目前在用的Dapper

与FrameWork不同,NetCore数据库配置需要从appsettings.json中获取

刚接触的时候被这块坑了,自己手动建了个app.config。然后你懂的(笑哭)

读取配置文件:

 public class AppSetting
{
private static readonly object objLock = new object();
private static AppSetting instance = null; private IConfigurationRoot Config { get; } private AppSetting()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
Config = builder.Build();
} public static AppSetting GetInstance()
{
if (instance == null)
{
lock (objLock)
{
if (instance == null)
{
instance = new AppSetting();
}
}
} return instance;
} public static string GetConfig(string name)
{
if (string.IsNullOrWhiteSpace(name))
{
return "name is null";
}
return GetInstance().Config.GetSection(name).Value;
}
}

日常够用的几个方法:

         /// <summary>
/// 返回行数
/// </summary>
/// <param name="sql">SQL</param>
/// <returns></returns>
public static int Execute(string sql)
{
string URL = AppSetting.GetConfig("ConnectionStringsDapper:DB");
IDbConnection conn = new SqlConnection(URL);
int result = conn.Execute(sql);
return result;
} /// <summary>
/// 返回List
/// </summary>
/// <param name="sql">SQL</param>
/// <returns></returns>
public static dynamic Query(string sql)
{
string URL = AppSetting.GetConfig("ConnectionStringsDapper:DB");
IDbConnection conn = new SqlConnection(URL);
dynamic result = conn.Query(sql);
return result;
} /// <summary>
/// 返回单条
/// </summary>
/// <param name="sql">SQL</param>
/// <returns></returns>
public static dynamic QueryFirst(string sql)
{
string URL = AppSetting.GetConfig("ConnectionStringsDapper:DB");
IDbConnection conn = new SqlConnection(URL);
dynamic result = conn.QueryFirst(sql);
return result;
}

Controller中调用:

       string sql = "bula bula bula";
  dynamic result = Query(sql);       string sql = "bula bula bula";
    dynamic result = QueryFirst(sql);       string sql = "bula bula bula";
   int result= Execute(sql);

Re0:在.NetCore 中Dapper的基本用法的更多相关文章

  1. Re0:在 .NetCore中 EF的基本使用

    整理一下目前在用的EFCore 记得好像是因为懒得写sql,于是开始试着用EF 先根据数据库生成一个好东西,嗯 Scaffold-DbContext "Data Source=localho ...

  2. netcore中的缓存介绍

    Cache(缓存)是优化web应用的常用方法,缓存存放在服务端的内存中,被所有用户共享.由于Cache存放在服务器的内存中,所以用户获取缓存资源的速度远比从服务器硬盘中获取快,但是从资源占有的角度考虑 ...

  3. Stackoverflow/dapper的Dapper-Extensions用法(二)

    之前翻译了Dapper-Extensions项目首页的readme.md,大家应该对这个类库的使用有一些了解了吧,接下来是wiki的文档翻译,主要提到了AutoClassMapper.KeyTypes ...

  4. Stackoverflow/dapper的Dapper-Extensions用法(一)

    Dapper-Extensions Dapper Extensions is a small library that complements Dapper by adding basic CRUD ...

  5. dapper的Dapper-Extensions用法(一)

    dapper的Dapper-Extensions用法(一) Dapper-Extensions Dapper Extensions is a small library that complement ...

  6. Asp.NetCore 中Aop的应用

    前言 其实好多项目中,做一些数据拦截.数据缓存都有Aop的概念,只是实现方式不一样:之前大家可能都会利用过滤器来实现Aop的功能,如果是Asp.NetCore的话,也可能会使用中间件: 而这种实现方式 ...

  7. .NetCore中的日志(2)集成第三方日志工具

    .NetCore中的日志(2)集成第三方日志工具 0x00 在.NetCore的Logging组件中集成NLog 上一篇讨论了.NetCore中日志框架的结构,这一篇讨论一下.NetCore的Logg ...

  8. .NetCore中的日志(1)日志组件解析

    .NetCore中的日志(1)日志组件解析 0x00 问题的产生 日志记录功能在开发中很常用,可以记录程序运行的细节,也可以记录用户的行为.在之前开发时我一般都是用自己写的小工具来记录日志,输出目标包 ...

  9. Java中的Socket的用法

                                   Java中的Socket的用法 Java中的Socket分为普通的Socket和NioSocket. 普通Socket的用法 Java中的 ...

随机推荐

  1. 部署logstash节点

    .部署Logstash节点 1.查看系统环境: [root@Logstash ~]# hostname Logstash [root@Logstash ~]# cat /etc/redhat-rele ...

  2. Springboot AOP写操作日志 GET POST

    pom.xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  3. [2019HDU多校第五场][HDU 6626][C. geometric problem]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6626 题目大意:给出平面上六个点\(A,B,M,N,X,Y\)以及两条直线\(L1,L2\),要求在四 ...

  4. js访问数据库

    一.js访问数据库的一般步骤: 1. 创建一个到数据库的 ADO 连接 conn = new ActiveXObject("ADODB.Connection"); 2. 打开数据库 ...

  5. vs 2017 无法安装任何 nuget package,提示“库没有注册。。。”

    vs 2017 无法安装任何 nuget package,提示“库没有注册(异常来自 HRESULT: 0x8002801D (TYPE_E_LIBNOTREGISTERED))” 各种百度谷歌都没有 ...

  6. 纯 css 控制隔行变色

    使用::nth-child 选择器 tr:nth-child(odd) { background-color: #ccc; } tr:nth-child(even) { background-colo ...

  7. 富文本编辑器复制word

    这种方法是servlet,编写好在web.xml里配置servlet-class和servlet-mapping即可使用 后台(服务端)java服务代码:(上传至ROOT/lqxcPics文件夹下) ...

  8. 动手动脑(ppt中6个问题)

    问题一:仔细阅读示例: EnumTest.java,运行它,分析运行结果? public class EnumTest { public static void main(String[] args) ...

  9. FOFA 批量采集url 图形化界面编写

    这是脚本 # coding:utf- import requests,re import time import sys import getopt import base64 guizhe='' s ...

  10. ubuntu dnsmasq问题

    在很多ubuntu开启wifi热点的教程中,配置比较繁琐的是hostapd+dnsmasq,很多教程都给出了dnsmasq的安装过程,其实在ubuntu桌面版系统下,已经集成到NetworkManag ...