• Re0:在.NetCore 中Dapper的基本用法


    整理一下目前在用的Dapper

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

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

    读取配置文件:

     1 public class AppSetting
     2     {
     3         private static readonly object objLock = new object();
     4         private static AppSetting instance = null;
     5 
     6         private IConfigurationRoot Config { get; }
     7 
     8         private AppSetting()
     9         {
    10             var builder = new ConfigurationBuilder()
    11              .SetBasePath(Directory.GetCurrentDirectory())
    12              .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
    13             Config = builder.Build();
    14         }
    15 
    16         public static AppSetting GetInstance()
    17         {
    18             if (instance == null)
    19             {
    20                 lock (objLock)
    21                 {
    22                     if (instance == null)
    23                     {
    24                         instance = new AppSetting();
    25                     }
    26                 }
    27             }
    28 
    29             return instance;
    30         }
    31 
    32         public static string GetConfig(string name)
    33         {
    34             if (string.IsNullOrWhiteSpace(name))
    35             {
    36                 return "name is null";
    37             }
    38             return GetInstance().Config.GetSection(name).Value;
    39         }
    40     }

    日常够用的几个方法:

     1         /// <summary>
     2         /// 返回行数
     3         /// </summary>
     4         /// <param name="sql">SQL</param>
     5         /// <returns></returns>
     6         public static int Execute(string sql)
     7         {
     8             string URL = AppSetting.GetConfig("ConnectionStringsDapper:DB");
     9             IDbConnection conn = new SqlConnection(URL);
    10             int result = conn.Execute(sql);
    11             return result;
    12         }
    13 
    14         /// <summary>
    15         /// 返回List
    16         /// </summary>
    17         /// <param name="sql">SQL</param>
    18         /// <returns></returns>
    19         public static dynamic Query(string sql)
    20         {
    21             string URL = AppSetting.GetConfig("ConnectionStringsDapper:DB");
    22             IDbConnection conn = new SqlConnection(URL);
    23             dynamic result = conn.Query(sql);
    24             return result;
    25         }
    26 
    27         /// <summary>
    28         /// 返回单条
    29         /// </summary>
    30         /// <param name="sql">SQL</param>
    31         /// <returns></returns>
    32         public static dynamic QueryFirst(string sql)
    33         {
    34             string URL = AppSetting.GetConfig("ConnectionStringsDapper:DB");
    35             IDbConnection conn = new SqlConnection(URL);
    36             dynamic result = conn.QueryFirst(sql);
    37             return result;
    38         }            

    Controller中调用:

    1     
    2       string sql = "bula bula bula";
    3          dynamic result = Query(sql);
    4 
    5       string sql = "bula bula bula";
    6         dynamic result = QueryFirst(sql);
    7 
    8       string sql = "bula bula bula";
    9          int result= Execute(sql);
  • 相关阅读:
    exFAT移动硬盘写保护怎么去掉
    ORACLE:一列的多行数据拼成字符串
    cxgrid中,如何根据列名或字段名取得footer值
    Delphi天气预报查询
    Delphi ListView基本用法大全
    datasnap的初步
    Delphi ListView基本用法大全
    Delphi实现树型结构
    获取身份证号码信息
    让Delphi XE5跟其他版本的Delphi共存
  • 原文地址:https://www.cnblogs.com/Ginease/p/10671593.html
Copyright © 2020-2023  润新知