SugarBase.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using SqlSugar;
  2. using System;
  3. using System.Data;
  4. using System.Threading;
  5. using WCS.Data.Utils;
  6. namespace WCS.Data
  7. {
  8. /// <summary>
  9. /// Sugar ORM父类, 封装一些基本的操作
  10. /// </summary>
  11. public class SugarBase
  12. {
  13. public static SqlSugarClient DB
  14. {
  15. get
  16. {
  17. return InitDB(30, SqlSugar.DbType.SqlServer, true);
  18. }
  19. }
  20. /// <summary>
  21. /// 数据库连接字符串, 在配置文件中的connectionStrings节点中添加name为SqlSugar的节点信息即可, 会自动获取
  22. /// </summary>
  23. public static string DBConnectionString { private get; set; }
  24. /// <summary>
  25. /// 获得SqlSugarClient(使用该方法, 默认请手动释放资源, 如using(var db = SugarBase.GetIntance()){你的代码}, 如果把isAutoCloseConnection参数设置为true, 则无需手动释放, 会每次操作数据库释放一次, 可能会影响性能, 请自行判断使用)
  26. /// </summary>
  27. /// <param name="commandTimeOut">等待超时时间, 默认为30秒 (单位: 秒)</param>
  28. /// <param name="dbType">数据库类型, 默认为SQL Server</param>
  29. /// <param name="isAutoCloseConnection">是否自动关闭数据库连接, 默认不是, 如果设置为true, 则会在每次操作完数据库后, 即时关闭, 如果一个方法里面多次操作了数据库, 建议保持为false, 否则可能会引发性能问题</param>
  30. /// <returns></returns>
  31. /// <author>旷丽文</author>
  32. public static SqlSugarClient GetIntance(int commandTimeOut = 30, SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer, bool isAutoCloseConnection = false)
  33. {
  34. return InitDB(commandTimeOut, dbType, isAutoCloseConnection);
  35. }
  36. /// <summary>
  37. /// 初始化ORM连接对象, 一般无需调用, 除非要自己写很复杂的数据库逻辑
  38. /// </summary>
  39. /// <param name="commandTimeOut">等待超时时间, 默认为30秒 (单位: 秒)</param>
  40. /// <param name="dbType">数据库类型, 默认为SQL Server</param>
  41. /// <param name="isAutoCloseConnection">是否自动关闭数据库连接, 默认不是, 如果设置为true, 则会在每次操作完数据库后, 即时关闭, 如果一个方法里面多次操作了数据库, 建议保持为false, 否则可能会引发性能问题</param>
  42. /// <author>旷丽文</author>
  43. private static SqlSugarClient InitDB(int commandTimeOut = 30, SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer, bool isAutoCloseConnection = false)
  44. {
  45. var db = new SqlSugarClient(new ConnectionConfig()
  46. {
  47. ConnectionString = DBConnectionString,
  48. DbType = dbType,
  49. InitKeyType = InitKeyType.Attribute,
  50. IsAutoCloseConnection = isAutoCloseConnection
  51. });
  52. db.Ado.CommandTimeOut = commandTimeOut;
  53. db.Aop.OnLogExecuting = (sql, pars) =>
  54. {
  55. string result = sql + "\r\n";// + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
  56. //if (result.ToLower().Contains("wcs_task"))
  57. //{
  58. // Current.Logger_Info.InfoFormat(result + ":" + resultParam);
  59. //}
  60. };
  61. return db;
  62. }
  63. /// <summary>
  64. /// 执行数据库操作
  65. /// </summary>
  66. /// <typeparam name="Result">返回值类型</typeparam>
  67. /// <param name="func">方法体</param>
  68. /// <returns></returns>
  69. /// <author>旷丽文</author>
  70. public static Result Exec<Result>(Func<SqlSugarClient, Result> func, int commandTimeOut = 30, SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer)
  71. {
  72. if (func == null) throw new Exception("委托为null, 事务处理无意义");
  73. using (var db = InitDB(commandTimeOut, dbType))
  74. {
  75. try
  76. {
  77. return func(db);
  78. }
  79. catch (Exception ex)
  80. {
  81. throw ex;
  82. }
  83. finally
  84. {
  85. db.Dispose();
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// 带事务处理的执行数据库操作
  91. /// </summary>
  92. /// <typeparam name="Result">返回值类型</typeparam>
  93. /// <param name="func">方法体</param>
  94. /// <returns></returns>
  95. /// <author>旷丽文</author>
  96. public static Result ExecTran<Result>(Func<SqlSugarClient, Result> func, int commandTimeOut = 30, SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer)
  97. {
  98. if (func == null) throw new Exception("委托为null, 事务处理无意义");
  99. using (var db = InitDB(commandTimeOut, dbType))
  100. {
  101. try
  102. {
  103. db.Ado.BeginTran(IsolationLevel.Unspecified);
  104. var result = func(db);
  105. db.Ado.CommitTran();
  106. return result;
  107. }
  108. catch (Exception ex)
  109. {
  110. db.Ado.RollbackTran();
  111. throw ex;
  112. }
  113. finally
  114. {
  115. db.Dispose();
  116. }
  117. }
  118. }
  119. }
  120. public class TryCachHelper
  121. {
  122. public static void TryExecute(Action action)
  123. {
  124. try
  125. {
  126. action();
  127. }
  128. catch (Exception ex)
  129. {
  130. LogMessageHelper.RecordLogMessage(ex);
  131. }
  132. finally
  133. {
  134. }
  135. }
  136. public static string TryExecute2(Action action)
  137. {
  138. string result = string.Empty;
  139. try
  140. {
  141. action();
  142. }
  143. catch (Exception ex)
  144. {
  145. result = ex.Message;
  146. LogMessageHelper.RecordLogMessage(ex);
  147. }
  148. finally
  149. {
  150. }
  151. return result;
  152. }
  153. /// <summary>
  154. /// 无事务无返回值的通用代码
  155. /// </summary>
  156. /// <param name="action"></param>
  157. /// <returns></returns>
  158. public static string TryExecute(Action<SqlSugarClient> action)
  159. {
  160. string msg = string.Empty;
  161. SqlSugarClient db = null;
  162. try
  163. {
  164. db = SugarBase.GetIntance();
  165. action(db);
  166. }
  167. catch (Exception ex)
  168. {
  169. msg = ex.Message;
  170. LogMessageHelper.RecordLogMessage(ex);
  171. }
  172. finally
  173. {
  174. if (db != null) ((IDisposable)db).Dispose();
  175. }
  176. return msg;
  177. }
  178. /// <summary>
  179. /// 无事务的有返回值通用代码
  180. /// </summary>
  181. /// <param name="func"></param>
  182. /// <returns></returns>
  183. public static string TryFuncExecute(Func<SqlSugarClient, string> func)
  184. {
  185. string msg = string.Empty;
  186. SqlSugarClient db = null;
  187. try
  188. {
  189. db = SugarBase.GetIntance();
  190. msg = func(db);
  191. }
  192. catch (Exception ex)
  193. {
  194. msg = ex.Message;
  195. LogMessageHelper.RecordLogMessage(ex);
  196. }
  197. finally
  198. {
  199. if (db != null) ((IDisposable)db).Dispose();
  200. }
  201. return msg;
  202. }
  203. public static void TryExecute(Action<SqlSugarClient> action, string methodName)
  204. {
  205. SqlSugarClient db = null;
  206. try
  207. {
  208. db = SugarBase.GetIntance();
  209. action(db);
  210. }
  211. catch (Exception ex)
  212. {
  213. LogMessageHelper.RecordLogMessage(ex);
  214. }
  215. finally
  216. {
  217. if (db != null) ((IDisposable)db).Dispose();
  218. }
  219. }
  220. /// <summary>
  221. /// 有事务的无返回值通用代码
  222. /// </summary>
  223. /// <param name="action"></param>
  224. /// <returns></returns>
  225. public static string TryTranExecute(Action<SqlSugarClient> action)
  226. {
  227. string msg = string.Empty;
  228. SqlSugarClient db = null;
  229. try
  230. {
  231. db = SugarBase.GetIntance();
  232. db.Ado.BeginTran();
  233. action(db);
  234. db.Ado.CommitTran();
  235. }
  236. catch (Exception ex)
  237. {
  238. msg = ex.Message;
  239. if (db != null) db.Ado.RollbackTran();
  240. LogMessageHelper.RecordLogMessage(ex);
  241. }
  242. finally
  243. {
  244. if (db != null) ((IDisposable)db).Dispose();
  245. }
  246. return msg;
  247. }
  248. /// <summary>
  249. /// 有事务的无返回值通用代码
  250. /// </summary>
  251. /// <param name="action"></param>
  252. /// <returns></returns>
  253. public static string TryTranExecute(Action<SqlSugarClient> action, int loc)
  254. {
  255. string msg = string.Empty;
  256. if (Interlocked.Exchange(ref loc, 1) == 0)
  257. {
  258. SqlSugarClient db = null;
  259. try
  260. {
  261. db = SugarBase.GetIntance();
  262. db.Ado.BeginTran();
  263. action(db);
  264. db.Ado.CommitTran();
  265. }
  266. catch (Exception ex)
  267. {
  268. msg = ex.Message;
  269. if (db != null) db.Ado.RollbackTran();
  270. LogMessageHelper.RecordLogMessage(ex);
  271. }
  272. finally
  273. {
  274. if (db != null) ((IDisposable)db).Dispose();
  275. Interlocked.Exchange(ref loc, 0);
  276. }
  277. }
  278. return msg;
  279. }
  280. /// <summary>
  281. /// 有事务的有返回值通用代码
  282. /// </summary>
  283. /// <param name="action"></param>
  284. /// <returns></returns>
  285. public static object TryTranFuncExecute(Func<SqlSugarClient, object> func)
  286. {
  287. object obj = null;
  288. SqlSugarClient db = null;
  289. try
  290. {
  291. db = SugarBase.GetIntance();
  292. db.Ado.BeginTran();
  293. obj = func(db);
  294. db.Ado.CommitTran();
  295. }
  296. catch (Exception ex)
  297. {
  298. obj = null;
  299. if (db != null) db.Ado.RollbackTran();
  300. LogMessageHelper.RecordLogMessage(ex);
  301. }
  302. finally
  303. {
  304. if (db != null) ((IDisposable)db).Dispose();
  305. }
  306. return obj;
  307. }
  308. public static void TryTranExecute(Action<SqlSugarClient> action, string methodName)
  309. {
  310. SqlSugarClient db = null;
  311. try
  312. {
  313. db = SugarBase.GetIntance();
  314. db.Ado.BeginTran();
  315. action(db);
  316. db.Ado.CommitTran();
  317. }
  318. catch (Exception ex)
  319. {
  320. if (db != null) db.Ado.RollbackTran();
  321. LogMessageHelper.RecordLogMessage(ex);
  322. }
  323. finally
  324. {
  325. if (db != null) ((IDisposable)db).Dispose();
  326. }
  327. }
  328. }
  329. }