zoukankan      html  css  js  c++  java
  • Dapper在.Net中的使用(二)

    配置和通用方法

    1、获取数据库配置和配置数据连接

     1         /// <summary>
     2         /// 打开Oracle链接
     3         /// </summary>
     4         /// <returns></returns>
     5         public static OracleConnection OpenOracleConnection()
     6         {
     7             return OpenOracleConnection(DapperOracleEnum.BasicConnecting);
     8         }
     9 
    10         /// <summary>
    11         /// 打开Oracle链接
    12         /// </summary>
    13         /// <param name="oracleEnum"></param>
    14         /// <returns></returns>
    15         public static OracleConnection OpenOracleConnection(DapperOracleEnum oracleEnum)
    16         {
    17             switch ((int)oracleEnum)
    18             { 
    19                 default:
    20                     var connection = new OracleConnection(BasicConnecting);
    21                     connection.Open();
    22                     return connection;
    23             }
    24         }

    2、查询

      1         /// <summary>
      2         /// 获取列表
      3         /// </summary>
      4         /// <typeparam name="T"></typeparam>
      5         /// <param name="sql"></param>
      6         /// <returns></returns>
      7         public static List<T> GetList<T>(string sql)
      8         {
      9             using (IDbConnection conn = OpenOracleConnection())
     10             {
     11                 try
     12                 {
     13                     var result = conn.Query<T>(sql);
     14                     if (result != null)
     15                     {
     16                         return result.ToList();
     17                     }
     18                 }
     19                 catch (Exception ex)
     20                 {
     21                     NlogHelper.WtiteDapper("Dapper", "GetList", sql, ex.Message);
     22                     throw new Exception(ex.Message);
     23                 }
     24             }
     25             return new List<T>();
     26         }
     27 
     28         /// <summary>
     29         /// 获取列表
     30         /// </summary>
     31         /// <typeparam name="T"></typeparam>
     32         /// <param name="sql"></param>
     33         /// <param name="oracleEnum"></param>
     34         /// <returns></returns>
     35         public static List<T> GetList<T>(string sql, DapperOracleEnum oracleEnum)
     36         {
     37             using (IDbConnection conn = OpenOracleConnection(oracleEnum))
     38             {
     39                 try
     40                 {
     41                     var result = conn.Query<T>(sql);
     42                     if (result != null)
     43                     {
     44                         return result == null ? new List<T>() : result.ToList();
     45                     }
     46                 }
     47                 catch (Exception ex)
     48                 {
     49                     NlogHelper.WtiteDapper("Dapper", "GetList", sql, ex.Message);
     50                     throw new Exception(ex.Message);
     51                 }
     52             }
     53             return new List<T>();
     54         }
     55 
     56         /// <summary>
     57         /// 获取详情
     58         /// </summary>
     59         /// <typeparam name="T"></typeparam>
     60         /// <param name="sql"></param>
     61         /// <returns></returns>
     62         public static T GetDetail<T>(string sql) where T : new()
     63         {
     64             using (IDbConnection conn = OpenOracleConnection())
     65             {
     66                 try
     67                 {
     68                     var result = conn.Query<T>(sql);
     69                     if (result != null)
     70                     {
     71                         return result == null ? new T() : result.ToList().FirstOrDefault();
     72                     }
     73                 }
     74                 catch (Exception ex)
     75                 {
     76                     NlogHelper.WtiteDapper("Dapper", "GetDetail", sql, ex.Message);
     77                     throw new Exception(ex.Message);
     78                 }
     79             }
     80             return new T();
     81         }
     82         /// <summary>
     83         /// 获取详情
     84         /// </summary>
     85         /// <typeparam name="T"></typeparam>
     86         /// <param name="sql"></param>
     87         /// <param name="oracleEnum"></param>
     88         /// <returns></returns>
     89         public static T GetDetail<T>(string sql, DapperOracleEnum oracleEnum) where T : new()
     90         {
     91             using (IDbConnection conn = OpenOracleConnection(oracleEnum))
     92             {
     93                 try
     94                 {
     95                     var result = conn.Query<T>(sql);
     96                     if (result != null)
     97                     {
     98                         return result == null ? new T() : result.ToList().FirstOrDefault();
     99                     }
    100                 }
    101                 catch (Exception ex)
    102                 {
    103                     NlogHelper.WtiteDapper("Dapper", "GetDetail", sql, ex.Message);
    104                     throw new Exception(ex.Message);
    105                 }
    106             }
    107             return new T();
    108         }
    109 
    110         /// <summary>
    111         /// 获取列的值
    112         /// </summary> 
    113         /// <param name="sql"></param>
    114         /// <returns></returns>
    115         public static object GetScalar(string sql)
    116         {
    117             using (IDbConnection conn = OpenOracleConnection())
    118             {
    119                 try
    120                 {
    121                     object result = conn.ExecuteScalar(sql);
    122                     if (result != null)
    123                     {
    124                         return result;
    125                     }
    126                 }
    127                 catch (Exception ex)
    128                 {
    129                     NlogHelper.WtiteDapper("Dapper", "GetScalar", sql, ex.Message);
    130                     throw new Exception(ex.Message);
    131                 }
    132             }
    133             return null;
    134         } 

    3、更新

      1         /// <summary>
      2         /// 更新
      3         /// </summary>
      4         /// <param name="sql"></param>
      5         /// <returns></returns>
      6         public static int Edit(string sql)
      7         {
      8             using (IDbConnection conn = OpenOracleConnection())
      9             {
     10                 try
     11                 {
     12                     var result = conn.Execute(sql);
     13                     if (result == 0) return 0;
     14                 }
     15                 catch (Exception ex)
     16                 {
     17                     NlogHelper.WtiteDapper("Dapper", "Edit", sql, ex.Message);
     18                     throw new Exception(ex.Message);
     19                 }
     20             }
     21             return 1;
     22         }
     23 
     24         /// <summary>
     25         /// 更新
     26         /// </summary>
     27         /// <param name="dapper"></param>
     28         /// <returns></returns>
     29         public static int Edit(DapperModel dapper)
     30         {
     31             using (IDbConnection conn = OpenOracleConnection())
     32             {
     33                 try
     34                 {
     35                     var result = conn.Execute(dapper.Sql, dapper.Param);
     36                     if (result == 0) return 0;
     37                 }
     38                 catch (Exception ex)
     39                 {
     40                     NlogHelper.WtiteDapper("Dapper", "Edit", dapper, ex.Message);
     41                     throw new Exception(ex.Message);
     42                 }
     43             }
     44             return 1;
     45         }
     46 
     47         /// <summary>
     48         /// 批量更新
     49         /// </summary>
     50         /// <param name="sqlList"></param>
     51         /// <returns></returns>
     52         public static int EditList(List<string> sqlList)
     53         {
     54             using (IDbConnection conn = OpenOracleConnection())
     55             {
     56                 string sql = string.Empty;
     57                 try
     58                 {
     59                     IDbTransaction transaction = conn.BeginTransaction();
     60                     var result = 0;
     61                     foreach (var item in sqlList)
     62                     {
     63                         sql = item;
     64                         result += conn.Execute(item, transaction);
     65                     }
     66                     transaction.Commit();
     67                     //if (result != sqlList.Count) return 0;
     68                 }
     69                 catch (Exception ex)
     70                 {
     71                     NlogHelper.WtiteDapper("Dapper", "EditList", sql, ex.Message);
     72                     throw new Exception(ex.Message);
     73                 }
     74             }
     75             return 1;
     76         }
     77 
     78         /// <summary>
     79         /// 批量更新
     80         /// </summary>
     81         /// <param name="dapperList"></param>
     82         /// <returns></returns>
     83         public static int EditList(List<DapperModel> dapperList)
     84         {
     85             using (IDbConnection conn = OpenOracleConnection())
     86             {
     87                 DapperModel sql = new DapperModel();//记录错误的语句
     88                 try
     89                 {
     90                     IDbTransaction transaction = conn.BeginTransaction();
     91                     var result = 0;
     92                     foreach (var dapper in dapperList)
     93                     {
     94                         sql = dapper;
     95                         result += conn.Execute(dapper.Sql, dapper.Param, transaction);
     96                     }
     97                     transaction.Commit();
     98                     //if (result != dapperList.Count) return 0;
     99                 }
    100                 catch (Exception ex)
    101                 {
    102                     NlogHelper.WtiteDapper("Dapper", "EditList", sql.ToJson(), ex.Message);
    103                     throw new Exception(ex.Message);
    104                 }
    105             }
    106             return 1;
    107         } 

    4、删除

     1         /// <summary>
     2         /// 删除
     3         /// </summary>
     4         /// <param name="sql"></param>
     5         /// <returns></returns>
     6         public static int Del(string sql)
     7         {
     8             using (IDbConnection conn = OpenOracleConnection())
     9             {
    10                 try
    11                 {
    12                     var result = conn.Execute(sql);
    13                     if (result == 0) return 0;
    14                 }
    15                 catch (Exception ex)
    16                 {
    17                     NlogHelper.WtiteDapper("Dapper", "Edit", sql, ex.Message);
    18                     throw new Exception(ex.Message);
    19                 }
    20             }
    21             return 1;
    22         }

    5、获取下一个序列

     1         /// <summary>
     2         /// 获取下一个序列
     3         /// </summary>
     4         /// <param name="sequenceName"></param>
     5         /// <returns></returns>
     6         public static int NextSequence(string sequenceName)
     7         {
     8             string sql = $@"select {sequenceName}.nextval as sequencevalue from dual";
     9             var sequence = GetScalar(sql);
    10             if (sequence != null) return sequence.ToInt();
    11             return -1;
    12         }

    6、调用存储过程

     1         /// <summary>
     2         /// 调用存储过程
     3         /// </summary>
     4         /// <param name="sql"></param> 
     5         /// <returns></returns>
     6         public static object GetCommand(string sql)
     7         {
     8             return GetCommand(sql, null);
     9         }
    10         /// <summary>
    11         /// 调用存储过程
    12         /// </summary>
    13         /// <param name="sql"></param>
    14         /// <param name="parames"></param> 
    15         /// <returns></returns>
    16         public static object GetCommand(string sql, DynamicParameters parames)
    17         {
    18             return GetCommand(sql, parames, CommandType.StoredProcedure);
    19         }
    20         /// <summary>
    21         /// 调用存储过程
    22         /// </summary>
    23         /// <param name="sql"></param>
    24         /// <param name="parames"></param>
    25         /// <param name="type"></param>
    26         /// <returns></returns>
    27         public static object GetCommand(string sql, DynamicParameters parames, CommandType type)
    28         {
    29             using (IDbConnection conn = OpenOracleConnection())
    30             {
    31                 try
    32                 {
    33                     object result = conn.Query(sql, parames, null, true, null, type); ;
    34                     if (result != null)
    35                     {
    36                         return result;
    37                     }
    38                 }
    39                 catch (Exception ex)
    40                 {
    41                     NlogHelper.WtiteDapper("Dapper", "GetCommand", sql, ex.Message);
    42                     throw new Exception(ex.Message);
    43                 }
    44             }
    45             return null;
    46         } 

    7、实体

     1     /// <summary>
     2     /// Dapper操作
     3     /// </summary>
     4     public class DapperModel
     5     {
     6         /// <summary>
     7         /// sql语句
     8         /// </summary>
     9         public string Sql { get; set; }
    10         /// <summary>
    11         /// 参数
    12         /// </summary>
    13         public object Param { get; set; }
    14     }
    15 
    16     /// <summary>
    17     /// 数据库类型
    18     /// </summary>
    19     public enum DapperOracleEnum
    20     {
    21         /// <summary>
    22         /// ITS_Basic
    23         /// </summary>
    24         BasicConnecting = 0,
    25     }

    备注NlogHelper是基于NLOG日志系统的帮助类

  • 相关阅读:
    C#与数据库访问技术总结(十一)之数据阅读器(DataReader)1
    C#与数据库访问技术总结(十)之添加&删除
    C#与数据库访问技术总结(九)之实例
    C#与数据库访问技术总结(八)之ExecuteNonQuery方法
    C#与数据库访问技术总结(六)之Command对象创建SQl语句代码示例
    C#与数据库访问技术总结(七)综合示例
    C#与数据库访问技术总结(五)之Command对象的常用方法
    OS——进程简答题(1)
    LAMP/LNMP 一键安装脚本
    运维如何延续自己的职业生涯--萧田国2017年GOPS深圳站演讲内容
  • 原文地址:https://www.cnblogs.com/raniynight/p/10450850.html
Copyright © 2011-2022 走看看