SqlSugarHelper.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using Microsoft.Data.SqlClient;
  2. using Newtonsoft.Json.Linq;
  3. using SqlSugar;
  4. using System.Data;
  5. namespace ServiceCenter.SqlSugars
  6. {
  7. /// <summary>
  8. /// ORM
  9. /// </summary>
  10. public class SqlSugarHelper
  11. {
  12. /// <summary>
  13. /// 数据库连接
  14. /// </summary>
  15. private static SqlSugarScope? _Db { get; set; } = null;
  16. /// <summary>
  17. /// 默认数据库连接Key
  18. /// </summary>
  19. private static string _Default { get; set; } = "";
  20. /// <summary>
  21. /// PLC据库连接Key
  22. /// </summary>
  23. private static string _PLCEX { get; set; } = "";
  24. /// <summary>
  25. /// PLC据库连接Key
  26. /// </summary>
  27. public static string _PLC { get; set; } = "";
  28. /// <summary>
  29. /// Dlog数据库连接Key
  30. /// </summary>
  31. private static string _Dlog { get; set; } = "";
  32. /// <summary>
  33. /// 设置数据库连接Key
  34. /// </summary>
  35. /// <param name="configId">默认多租户ID</param>
  36. public static void SetDefault(string configId)
  37. {
  38. _Default = configId;
  39. }
  40. /// <summary>
  41. /// 设置 Dlog数据库连接Key
  42. /// </summary>
  43. /// <param name="configId">多租户Dlog ID</param>
  44. public static void SetDlog(string configId)
  45. {
  46. _Dlog = configId;
  47. }
  48. /// <summary>
  49. /// 设置 plc数据库连接Key
  50. /// </summary>
  51. /// <param name="configId">多租户Dlog ID</param>
  52. public static void SetPLC(string configId)
  53. {
  54. _PLC = configId;
  55. }
  56. /// <summary>
  57. /// 设置 plc数据库连接Key
  58. /// </summary>
  59. /// <param name="configId">多租户Dlog ID</param>
  60. public static void SetPLCEX(string configId)
  61. {
  62. _PLCEX = configId;
  63. }
  64. /// <summary>
  65. /// 默认数据库连接Key
  66. /// </summary>
  67. public SqlSugarScopeProvider Default
  68. {
  69. get
  70. {
  71. if (_Default == "") throw new Exception("请调用[SqlSugarHelper.SetDefault]方法设置默认数据库连接");
  72. if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  73. return _Db.GetConnectionScope(_Default);
  74. }
  75. }
  76. /// <summary>
  77. /// Dlog数据库连接Key
  78. /// </summary>
  79. public SqlSugarScopeProvider Dlog
  80. {
  81. get
  82. {
  83. if (_Dlog == "") throw new Exception("请调用[SqlSugarHelper.SetDlog]方法设置默认数据库连接");
  84. if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  85. return _Db.GetConnectionScope(_Dlog);
  86. }
  87. }
  88. /// <summary>
  89. /// plc数据库连接Key
  90. /// </summary>
  91. public SqlSugarScopeProvider PLC
  92. {
  93. get
  94. {
  95. if (_PLC == "") throw new Exception("请调用[SqlSugarHelper.SetPLC]方法设置默认数据库连接");
  96. if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  97. return _Db.GetConnectionScope(_PLC);
  98. }
  99. }
  100. /// <summary>
  101. /// plc数据库连接Key
  102. /// </summary>
  103. public SqlSugarScopeProvider PLCEX
  104. {
  105. get
  106. {
  107. if (_PLCEX == "") throw new Exception("请调用[SqlSugarHelper.SetPLC]方法设置默认数据库连接");
  108. if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  109. return _Db.GetConnectionScope(_PLCEX);
  110. }
  111. }
  112. /// <summary>
  113. /// 设置数据库连接
  114. /// </summary>
  115. /// <param name="sqlSugarScope"></param>
  116. public static void SetDb(SqlSugarScope sqlSugarScope)
  117. {
  118. _Db = sqlSugarScope;
  119. }
  120. /// <summary>
  121. /// 数据库连接
  122. /// 注意需要
  123. /// </summary>
  124. public SqlSugarScope Connect
  125. {
  126. get
  127. {
  128. return _Db ?? throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  129. }
  130. }
  131. /// <summary>
  132. /// 执行事务
  133. /// </summary>
  134. /// <param name="act"></param>
  135. /// <exception cref="Exception"></exception>
  136. public static void Do(Action<SqlSugarHelper> act)
  137. {
  138. if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  139. var db = new SqlSugarHelper();
  140. try
  141. {
  142. db.Connect.BeginTran();//开始事务
  143. act(db);//执行委托
  144. db.Connect.CommitTran();//提交事务
  145. }
  146. catch (Exception ex)
  147. {
  148. db.Connect.RollbackTran();//回滚事务
  149. if (ex.Message.Contains("SqlTransaction")) throw new Exception($"{ex.Message}:{ex.StackTrace}");
  150. throw new Exception(ex.Message);
  151. }
  152. }
  153. /// <summary>
  154. /// 直接返回查询结果
  155. /// </summary>
  156. /// <typeparam name="T"></typeparam>
  157. /// <param name="act"></param>
  158. /// <returns></returns>
  159. /// <exception cref="Exception"></exception>
  160. public static T Do<T>(Func<SqlSugarHelper, T> act)
  161. {
  162. if (_Db == null) throw new Exception("请调用[SqlSugarHelper.SetDb]方法设置设置数据库连接");
  163. var db = new SqlSugarHelper();
  164. try
  165. {
  166. db.Connect.BeginTran();//开始事务
  167. db.Connect.Ado.CommandTimeOut = 10;
  168. var res = act(db);//执行委托
  169. db.Connect.CommitTran();//提交事务
  170. return res;
  171. }
  172. catch (Exception ex)
  173. {
  174. db.Connect.RollbackTran();//回滚事务
  175. if (ex.Message.Contains("SqlTransaction")) throw new Exception($"{ex.Message}:{ex.StackTrace}");
  176. throw new Exception(ex.Message);
  177. }
  178. }
  179. }
  180. }