SugarBase.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 = "Data Source=192.168.249.120;Initial Catalog=WCS_MW;Persist Security Info=True;User ID=sa;Pwd=password@123$%^",
  48. //ConnectionString = "Data Source=192.168.249.151;Initial Catalog=WCS_OPP;Persist Security Info=True;User ID=sa;Pwd=Password@123",
  49. DbType = dbType,
  50. InitKeyType = InitKeyType.Attribute,
  51. IsAutoCloseConnection = isAutoCloseConnection
  52. });
  53. db.Ado.CommandTimeOut = commandTimeOut;
  54. db.Aop.OnLogExecuting = (sql, pars) =>
  55. {
  56. string result = sql + "\r\n";// + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
  57. //if (result.ToLower().Contains("wcs_task"))
  58. //{
  59. // Current.Logger_Info.InfoFormat(result + ":" + resultParam);
  60. //}
  61. };
  62. return db;
  63. }
  64. /// <summary>
  65. /// 执行数据库操作
  66. /// </summary>
  67. /// <typeparam name="Result">返回值类型</typeparam>
  68. /// <param name="func">方法体</param>
  69. /// <returns></returns>
  70. /// <author>旷丽文</author>
  71. public static Result Exec<Result>(Func<SqlSugarClient, Result> func, int commandTimeOut = 30, SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer)
  72. {
  73. if (func == null) throw new Exception("委托为null, 事务处理无意义");
  74. using (var db = InitDB(commandTimeOut, dbType))
  75. {
  76. try
  77. {
  78. return func(db);
  79. }
  80. catch (Exception ex)
  81. {
  82. throw ex;
  83. }
  84. finally
  85. {
  86. db.Dispose();
  87. }
  88. }
  89. }
  90. /// <summary>
  91. /// 带事务处理的执行数据库操作
  92. /// </summary>
  93. /// <typeparam name="Result">返回值类型</typeparam>
  94. /// <param name="func">方法体</param>
  95. /// <returns></returns>
  96. /// <author>旷丽文</author>
  97. public static Result ExecTran<Result>(Func<SqlSugarClient, Result> func, int commandTimeOut = 30, SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer)
  98. {
  99. if (func == null) throw new Exception("委托为null, 事务处理无意义");
  100. using (var db = InitDB(commandTimeOut, dbType))
  101. {
  102. try
  103. {
  104. db.Ado.BeginTran(IsolationLevel.Unspecified);
  105. var result = func(db);
  106. db.Ado.CommitTran();
  107. return result;
  108. }
  109. catch (Exception ex)
  110. {
  111. db.Ado.RollbackTran();
  112. throw ex;
  113. }
  114. finally
  115. {
  116. db.Dispose();
  117. }
  118. }
  119. }
  120. }
  121. public class TryCachHelper
  122. {
  123. public static void TryExecute(Action action)
  124. {
  125. try
  126. {
  127. action();
  128. }
  129. catch (Exception ex)
  130. {
  131. LogMessageHelper.RecordLogMessage(ex);
  132. }
  133. finally
  134. {
  135. }
  136. }
  137. public static string TryExecute2(Action action)
  138. {
  139. string result = string.Empty;
  140. try
  141. {
  142. action();
  143. }
  144. catch (Exception ex)
  145. {
  146. result = ex.Message;
  147. LogMessageHelper.RecordLogMessage(ex);
  148. }
  149. finally
  150. {
  151. }
  152. return result;
  153. }
  154. /// <summary>
  155. /// 无事务无返回值的通用代码
  156. /// </summary>
  157. /// <param name="action"></param>
  158. /// <returns></returns>
  159. public static string TryExecute(Action<SqlSugarClient> action)
  160. {
  161. string msg = string.Empty;
  162. SqlSugarClient db = null;
  163. try
  164. {
  165. db = SugarBase.GetIntance();
  166. action(db);
  167. }
  168. catch (Exception ex)
  169. {
  170. msg = ex.Message;
  171. LogMessageHelper.RecordLogMessage(ex);
  172. }
  173. finally
  174. {
  175. if (db != null) ((IDisposable)db).Dispose();
  176. }
  177. return msg;
  178. }
  179. /// <summary>
  180. /// 无事务的有返回值通用代码
  181. /// </summary>
  182. /// <param name="func"></param>
  183. /// <returns></returns>
  184. public static string TryFuncExecute(Func<SqlSugarClient, string> func)
  185. {
  186. string msg = string.Empty;
  187. SqlSugarClient db = null;
  188. try
  189. {
  190. db = SugarBase.GetIntance();
  191. msg = func(db);
  192. }
  193. catch (Exception ex)
  194. {
  195. msg = ex.Message;
  196. LogMessageHelper.RecordLogMessage(ex);
  197. }
  198. finally
  199. {
  200. if (db != null) ((IDisposable)db).Dispose();
  201. }
  202. return msg;
  203. }
  204. public static void TryExecute(Action<SqlSugarClient> action, string methodName)
  205. {
  206. SqlSugarClient db = null;
  207. try
  208. {
  209. db = SugarBase.GetIntance();
  210. action(db);
  211. }
  212. catch (Exception ex)
  213. {
  214. LogMessageHelper.RecordLogMessage(ex);
  215. }
  216. finally
  217. {
  218. if (db != null) ((IDisposable)db).Dispose();
  219. }
  220. }
  221. /// <summary>
  222. /// 有事务的无返回值通用代码
  223. /// </summary>
  224. /// <param name="action"></param>
  225. /// <returns></returns>
  226. public static string TryTranExecute(Action<SqlSugarClient> action)
  227. {
  228. string msg = string.Empty;
  229. SqlSugarClient db = null;
  230. try
  231. {
  232. db = SugarBase.GetIntance();
  233. db.Ado.BeginTran();
  234. action(db);
  235. db.Ado.CommitTran();
  236. }
  237. catch (Exception ex)
  238. {
  239. msg = ex.Message;
  240. if (db != null) db.Ado.RollbackTran();
  241. LogMessageHelper.RecordLogMessage(ex);
  242. }
  243. finally
  244. {
  245. if (db != null) ((IDisposable)db).Dispose();
  246. }
  247. return msg;
  248. }
  249. /// <summary>
  250. /// 有事务的无返回值通用代码
  251. /// </summary>
  252. /// <param name="action"></param>
  253. /// <returns></returns>
  254. public static string TryTranExecute(Action<SqlSugarClient> action, int loc)
  255. {
  256. string msg = string.Empty;
  257. if (Interlocked.Exchange(ref loc, 1) == 0)
  258. {
  259. SqlSugarClient db = null;
  260. try
  261. {
  262. db = SugarBase.GetIntance();
  263. db.Ado.BeginTran();
  264. action(db);
  265. db.Ado.CommitTran();
  266. }
  267. catch (Exception ex)
  268. {
  269. msg = ex.Message;
  270. if (db != null) db.Ado.RollbackTran();
  271. LogMessageHelper.RecordLogMessage(ex);
  272. }
  273. finally
  274. {
  275. if (db != null) ((IDisposable)db).Dispose();
  276. Interlocked.Exchange(ref loc, 0);
  277. }
  278. }
  279. return msg;
  280. }
  281. /// <summary>
  282. /// 有事务的有返回值通用代码
  283. /// </summary>
  284. /// <param name="action"></param>
  285. /// <returns></returns>
  286. public static object TryTranFuncExecute(Func<SqlSugarClient, object> func)
  287. {
  288. object obj = null;
  289. SqlSugarClient db = null;
  290. try
  291. {
  292. db = SugarBase.GetIntance();
  293. db.Ado.BeginTran();
  294. obj = func(db);
  295. db.Ado.CommitTran();
  296. }
  297. catch (Exception ex)
  298. {
  299. obj = null;
  300. if (db != null) db.Ado.RollbackTran();
  301. LogMessageHelper.RecordLogMessage(ex);
  302. }
  303. finally
  304. {
  305. if (db != null) ((IDisposable)db).Dispose();
  306. }
  307. return obj;
  308. }
  309. public static void TryTranExecute(Action<SqlSugarClient> action, string methodName)
  310. {
  311. SqlSugarClient db = null;
  312. try
  313. {
  314. db = SugarBase.GetIntance();
  315. db.Ado.BeginTran();
  316. action(db);
  317. db.Ado.CommitTran();
  318. }
  319. catch (Exception ex)
  320. {
  321. if (db != null) db.Ado.RollbackTran();
  322. LogMessageHelper.RecordLogMessage(ex);
  323. }
  324. finally
  325. {
  326. if (db != null) ((IDisposable)db).Dispose();
  327. }
  328. }
  329. }
  330. }