DapperHelper.cs 8.7 KB

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