Dapper-MySQLHelper 的通用封装

 

  1 using MySql.Data.MySqlClient;
  2 using System;
  3 using System.Collections;
  4 using System.Collections.Generic;
  5 using System.Configuration;
  6 using System.Data.SqlClient;
  7 using System.Data;
  8 using System.Linq;
  9 using System.Text;
 10 using System.Threading.Tasks;
 11 using Dapper;
 12 using DapperExtensions;
 13 
 14 namespace DapperData
 15 {
 16     public class MySQLDapperHelper<T> where T:class
 17     {
 18         /// <summary>
 19         /// 数据库连接字符串
 20         /// </summary>
 21         private static readonly string connectionString =
 22             ConfigurationManager.ConnectionStrings["MySqlConn"].ConnectionString;
 23 
 24         /// <summary>
 25         /// 查询列表
 26         /// </summary>
 27         /// <param name="sql">查询的sql</param>
 28         /// <param name="param">替换参数</param>
 29         /// <returns></returns>
 30         public static List<T> Query(string sql,  object  param)
 31         {
 32             using (IDbConnection con = new MySqlConnection(connectionString))
 33             {
 34                 return con.Query<T>(sql, param).ToList();
 35             }
 36         } 
 37 
 38         /// <summary>
 39         /// 查询第一个数据
 40         /// </summary>
 41         /// <param name="sql"></param>
 42         /// <param name="param"></param>
 43         /// <returns></returns>
 44         public static T QueryFirst(string sql, object param)
 45         {
 46             using (IDbConnection con = new MySqlConnection(connectionString))
 47             {
 48                 return con.QueryFirst<T>(sql, param);
 49             }
 50         }
 51 
 52         /// <summary>
 53         /// 查询第一个数据没有返回默认值
 54         /// </summary>
 55         /// <param name="sql"></param>
 56         /// <param name="param"></param>
 57         /// <returns></returns>
 58         public static T QueryFirstOrDefault(string sql, object param)
 59         {
 60             using (IDbConnection con = new MySqlConnection(connectionString))
 61             {
 62                 return con.QueryFirstOrDefault<T>(sql, param);
 63             }
 64         }
 65 
 66         /// <summary>
 67         /// 查询单条数据
 68         /// </summary>
 69         /// <param name="sql"></param>
 70         /// <param name="param"></param>
 71         /// <returns></returns>
 72         public static T QuerySingle(string sql, object param)
 73         {
 74             using (IDbConnection con = new MySqlConnection(connectionString))
 75             {
 76                 return con.QuerySingle<T>(sql, param);
 77             }
 78         }
 79 
 80         /// <summary>
 81         /// 查询单条数据没有返回默认值
 82         /// </summary>
 83         /// <param name="sql"></param>
 84         /// <param name="param"></param>
 85         /// <returns></returns>
 86         public static T QuerySingleOrDefault(string sql, object param)
 87         {
 88             using (IDbConnection con = new MySqlConnection(connectionString))
 89             {
 90                 return con.QuerySingleOrDefault<T>(sql, param);
 91             }
 92         }
 93 
 94         /// <summary>
 95         /// 增删改
 96         /// </summary>
 97         /// <param name="sql"></param>
 98         /// <param name="param"></param>
 99         /// <returns></returns>
100         public static int Execute(string sql, object param)
101         {
102             using (IDbConnection con = new MySqlConnection(connectionString))
103             {
104                 return con.Execute(sql, param);
105             }
106         }
107 
108         /// <summary>
109         /// Reader获取数据
110         /// </summary>
111         /// <param name="sql"></param>
112         /// <param name="param"></param>
113         /// <returns></returns>
114         public static IDataReader ExecuteReader(string sql, object param)
115         {
116             using (IDbConnection con = new MySqlConnection(connectionString))
117             {
118                 return con.ExecuteReader(sql, param);
119             }
120         }
121 
122         /// <summary>
123         /// Scalar获取数据
124         /// </summary>
125         /// <param name="sql"></param>
126         /// <param name="param"></param>
127         /// <returns></returns>
128         public static object ExecuteScalar(string sql, object param)
129         {
130             using (IDbConnection con = new MySqlConnection(connectionString))
131             {
132                 return con.ExecuteScalar(sql, param);
133             }
134         }
135 
136         /// <summary>
137         /// Scalar获取数据
138         /// </summary>
139         /// <param name="sql"></param>
140         /// <param name="param"></param>
141         /// <returns></returns>
142         public static T ExecuteScalarForT(string sql, object param)
143         {
144             using (IDbConnection con = new MySqlConnection(connectionString))
145             {
146                 return con.ExecuteScalar<T>(sql, param);
147             }
148         }
149 
150         /// <summary>
151         /// 带参数的存储过程
152         /// </summary>
153         /// <param name="sql"></param>
154         /// <param name="param"></param>
155         /// <returns></returns>
156         public static List<T> ExecutePro(string proc, object param)
157         {
158             using (IDbConnection con = new MySqlConnection(connectionString))
159             {
160                 List<T> list = con.Query<T>(proc,
161                     param,
162                     null,
163                     true,
164                     null,
165                     CommandType.StoredProcedure).ToList();
166                 return list;
167             }
168         }
169 
170 
171         /// <summary>
172         /// 事务1 - 全SQL
173         /// </summary>
174         /// <param name="sqlarr">多条SQL</param>
175         /// <param name="param">param</param>
176         /// <returns></returns>
177         public static int ExecuteTransaction(string[] sqlarr)
178         {
179             using (IDbConnection con = new MySqlConnection(connectionString))
180             {
181                 using (var transaction = con.BeginTransaction())
182                 {
183                     try
184                     {
185                         int result = 0;
186                         foreach (var sql in sqlarr)
187                         {
188                             result += con.Execute(sql, null, transaction);
189                         }
190 
191                         transaction.Commit();
192                         return result;
193                     }
194                     catch (Exception ex)
195                     {
196                         transaction.Rollback();
197                         return 0;
198                     }
199                 }
200             }
201         }
202 
203         /// <summary>
204         /// 事务2 - 声明参数
205         ///demo:
206         ///dic.Add("Insert into Users values (@UserName, @Email, @Address)",
207         ///        new { UserName = "jack", Email = "380234234@qq.com", Address = "上海" });
208         /// </summary>
209         /// <param name="Key">多条SQL</param>
210         /// <param name="Value">param</param>
211         /// <returns></returns>
212         public static int ExecuteTransaction(Dictionary<string, object> dic)
213         {
214             using (IDbConnection con = new MySqlConnection(connectionString))
215             {
216                 using (var transaction = con.BeginTransaction())
217                 {
218                     try
219                     {
220                         int result = 0;
221                         foreach (var sql in dic)
222                         {
223                             result += con.Execute(sql.Key, sql.Value, transaction);
224                         }
225 
226                         transaction.Commit();
227                         return result;
228                     }
229                     catch (Exception ex)
230                     {
231                         transaction.Rollback();
232                         return 0;
233                     }
234                 }
235             }
236         }
237     }
238     
239 }

 

posted @ 2023-04-16 20:40  LowKeyC  阅读(209)  评论(0编辑  收藏  举报
有志者事竟成破釜沉舟百二秦关终属楚苦心人,天不负,卧薪尝胆,三千越甲可吞吴