SqlSugarHelper.cs 6.1 KB

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