SqlSugarHelper.cs 5.2 KB

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