安装使用Dapper和拓展QueryToDataTable方法

1、首先在解决方案里右键点击项目选择【管理NuGet程序包】,在里面搜索并安装Dapper。

 

 2、在项目中引用Dapper

 

 3、创建一个DbHelper帮助类,顺便拓展一下toDataTable 的方法

 1 using Dapper;
 2 namespace NetFramework.Model
 3 {
 4     public static class DbHelper
 5     {
 6         private static readonly string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["defaultNew"].ToString();
 7 
 8         //创建连接对象
 9         public static IDbConnection GetConn()
10         {
11             var conn = new OracleConnection(connectionString);
12             return conn;
13         }
14 
15         public static IDbConnection GetConnEmr()
16         {
17             throw new NotImplementedException();
18         }
19 /// <summary>
20         ///  返回查询结果集
21         /// </summary>
22         /// <param name="sql"></param>
23         /// <param name="type"></param>
24         /// <param name="param"></param>
25         /// <returns></returns>
26         public static DataTable QueryToDataTable(this IDbConnection connection, string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
27         {
28             using (var dataReader = connection.ExecuteReader(sql, param, transaction, commandTimeout, commandType))
29             {
30                 var datatable = new DataTable();
31                 for (int i = 0; i < dataReader.FieldCount; i++)
32                 {
33                     DataColumn mydc = new DataColumn();    //关键的一步
34                     mydc.DataType = dataReader.GetFieldType(i);
35                     mydc.ColumnName = dataReader.GetName(i);
36                     datatable.Columns.Add(mydc);          //关键的第二步
37                 }
38 
39                 while (dataReader.Read())
40                 {
41                     DataRow myDataRow = datatable.NewRow();
42                     for (int i = 0; i < dataReader.FieldCount; i++)
43                     {
44                         myDataRow[i] = dataReader[i].ToString();
45                     }
46                     datatable.Rows.Add(myDataRow);
47                 }
48                 ///关闭数据读取器
49                 dataReader.Close();
50                 return datatable;
51             }
52 
53         }
54     }
55 }

4、使用的话也很简单

 1 public static bool DealWithMatters(int status, string tfdh)
 2         {
 3 
 4             using (var conn = DbHelper.GetConn())
 5             {
 6                 conn.Open();//用事务的话需要手动打开
 7                 using (var transaction = conn.BeginTransaction())
 8                 {
 9                     try
10                     {
11                         string sql = $"UPDATE NET_MZTF SET STATUS='{status}' WHERE TFDH={tfdh}";
12                         bool i = conn.Execute(sql, transaction) > 0;
13                         sql = $"insert into NET_MZTF_1()values()";
14                         conn.Execute(sql, transaction);
15                         transaction.Commit();
16                         return true;
17                     }
18                     catch (Exception ex)
19                     {
20                         transaction.Rollback();
21                         MessageBox.Show(ex.Message);
22                     }
23                     finally
24                     {
25                         conn.Close();//记得关闭
26                     }
27                     return false;
28                 }
29             }
30         }
 1 public static DataTable GetFlowStatus(string operCode)
 2         {
 3             using (var conn = DbHelper.GetConn())
 4             {
 5                 string sql = $"select * from TabeleA where OPERCODE=:operCode";//连的是oracle的数据库,其他的数据库sql参数化不一样
 6                 var parameters = new DynamicParameters();
 7                 parameters.Add("operCode", operCode);
 8                 //自己拓展的ToDataTable方法
 9                 DataTable dt = conn.QueryToDataTable(sql, parameters);
10                 return dt;
11             }
12         }

5、Dapper通过扩展IDbConnection提供一些有用的扩展方法去查询您的数据库。详细的自行去看看Dapper的方法介绍。

 

posted @ 2022-06-23 09:59  进击的黑大帅  阅读(430)  评论(0编辑  收藏  举报