You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
9.9 KiB

2 years ago
  1. using System;
  2. using System.Data;
  3. namespace SqlSugar.Base
  4. {
  5. using log4net;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. /// <summary>
  10. /// Sugar ORM父類, 封裝一些基本的操作
  11. /// </summary>
  12. public static class SugarBase
  13. {
  14. public static readonly ILog mo_Log = LogManager.GetLogger("SQLSugarLog");//記錄異常
  15. /// <summary>
  16. /// 資料庫連結字串
  17. /// </summary>
  18. public static string DB_ConnectionString = GetConnectionString("ConnectionString");
  19. /// <summary>
  20. /// 獲取ORM資料庫連線物件(只操作資料庫一次的使用, 否則會進行多次資料庫連接和關閉) 默認超時時間為30秒 預設為SQL Server資料庫 預設自動關閉資料庫連結,
  21. /// 多次操作資料庫請勿使用該屬性, 可能會造成性能問題 要自訂請使用GetIntance()方法或者直接使用Exec方法, 傳委託
  22. /// </summary>
  23. public static SqlSugarClient DB
  24. {
  25. get
  26. {
  27. return InitDB(null, 30, DbType.SqlServer, true);
  28. }
  29. }
  30. /// <summary>
  31. /// 獲得SqlSugarClient(使用該方法, 默認請手動釋放資源, 如using(var db = SugarBase.GetIntance()){你的代碼},
  32. /// 如果把isAutoCloseConnection參數設置為true, 則無需手動釋放, 會每次操作資料庫釋放一次, 可能會影響性能, 請自行判斷使用)
  33. /// </summary>
  34. /// <param name="commandTimeOut">等待超時時間, 默認為30秒 (單位: 秒)</param>
  35. /// <param name="dbType">資料庫類型, 預設為SQL Server</param>
  36. /// <param name="isAutoCloseConnection">
  37. /// 是否自動關閉資料庫連接, 預設不是, 如果設置為true, 則會在每次操作完資料庫後, 即時關閉, 如果一個方法裡面多次操作了資料庫, 建議保持為false, 否則可能會引發性能問題
  38. /// </param>
  39. /// <returns></returns>
  40. public static SqlSugarClient GetIntance(List<SqlFuncExternal> expfn = null, int commandTimeOut = 30, DbType dbType = DbType.SqlServer, bool isAutoCloseConnection = false)
  41. {
  42. var db = SugarBase.InitDB(expfn, commandTimeOut, dbType, isAutoCloseConnection);
  43. db.Aop.OnLogExecuting = (sql, pars) =>
  44. {
  45. var temsql = sql;
  46. var dic = pars.ToDictionary(it => it.ParameterName, it => it.Value);
  47. foreach (var param in dic)
  48. {
  49. if (param.Value != null)
  50. {
  51. temsql = temsql.Replace(param.Key, "'" + param.Value.ToString() + "'");
  52. }
  53. }
  54. System.Diagnostics.Debug.WriteLine(temsql + "\r\n");
  55. //mo_Log.Info(temsql); //紀錄LOG打開此行
  56. };
  57. return db;
  58. }
  59. public static SqlSugarClient InstanceByAttribute
  60. {
  61. get
  62. {
  63. var db = new SqlSugarClient(new ConnectionConfig { InitKeyType = InitKeyType.Attribute, ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer, IsAutoCloseConnection = true });
  64. return db;
  65. }
  66. }
  67. /// <summary>
  68. /// 初始化ORM連線物件
  69. /// </summary>
  70. /// <param name="commandTimeOut">等待超時時間, 默認為30秒 (單位: 秒)</param>
  71. /// <param name="dbType">資料庫類型, 預設為SQL Server</param>
  72. /// <param name="isAutoCloseConnection">
  73. /// 是否自動關閉資料庫連接, 預設不是, 如果設置為true, 則會在每次操作完資料庫後, 即時關閉, 如果一個方法裡面多次操作了資料庫, 建議保持為false, 否則可能會引發性能問題
  74. /// </param>
  75. private static SqlSugarClient InitDB(List<SqlFuncExternal> expfn, int commandTimeOut = 30, DbType dbType = DbType.SqlServer, bool isAutoCloseConnection = false)
  76. {
  77. var expMethods = GetExpMethods(expfn);
  78. var db = new SqlSugarClient(new ConnectionConfig
  79. {
  80. ConnectionString = SugarBase.DB_ConnectionString,
  81. DbType = dbType,
  82. InitKeyType = InitKeyType.Attribute,
  83. IsAutoCloseConnection = isAutoCloseConnection,
  84. ConfigureExternalServices = new ConfigureExternalServices
  85. {
  86. SqlFuncServices = expMethods//set ext method
  87. }
  88. });
  89. db.Ado.CommandTimeOut = commandTimeOut;
  90. return db;
  91. }
  92. /// <summary>
  93. /// 執行資料庫操作
  94. /// </summary>
  95. /// <typeparam name="Result">返回數值型別 泛型</typeparam>
  96. /// <param name="func">方法委託</param>
  97. /// <param name="commandTimeOut">超時時間, 單位為秒, 預設為30秒</param>
  98. /// <param name="dbType">資料庫類型, 預設為SQL Server</param>
  99. /// <returns>泛型返回值</returns>
  100. public static Result Exec<Result>(Func<SqlSugarClient, Result> func, int commandTimeOut = 30, DbType dbType = DbType.SqlServer)
  101. {
  102. if (func == null) throw new Exception("委託為null, 交易處理無意義");
  103. using (var db = InitDB(null, commandTimeOut, dbType))
  104. {
  105. try
  106. {
  107. return func(db);
  108. }
  109. catch (Exception ex)
  110. {
  111. throw ex;
  112. }
  113. finally
  114. {
  115. db.Dispose();
  116. }
  117. }
  118. }
  119. /// <summary>
  120. /// 帶交易處理的執行資料庫操作
  121. /// </summary>
  122. /// <typeparam name="Result">返回數值型別 泛型</typeparam>
  123. /// <param name="func">方法委託</param>
  124. /// <param name="commandTimeOut">超時時間, 單位為秒, 預設為30秒</param>
  125. /// <param name="dbType">資料庫類型, 預設為SQL Server</param>
  126. /// <returns>泛型返回值</returns>
  127. public static Result ExecTran<Result>(Func<SqlSugarClient, Result> func, int commandTimeOut = 30, DbType dbType = DbType.SqlServer)
  128. {
  129. if (func == null) throw new Exception("委託為null, 交易處理無意義");
  130. using (var db = InitDB(null, commandTimeOut, dbType))
  131. {
  132. try
  133. {
  134. db.Ado.BeginTran(IsolationLevel.Unspecified);
  135. var result = func(db);
  136. db.Ado.CommitTran();
  137. return result;
  138. }
  139. catch (Exception ex)
  140. {
  141. db.Ado.RollbackTran();
  142. throw ex;
  143. }
  144. finally
  145. {
  146. db.Dispose();
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// 根據傳入的Key獲取設定檔中 相應Key的資料庫連接字串
  152. /// </summary>
  153. /// <param name="Key"></param>
  154. /// <returns></returns>
  155. public static string GetConnectionString(string Key)
  156. {
  157. try
  158. {
  159. var connectionString = DBUnit.GetAppSettings(Key);
  160. if (!String.IsNullOrEmpty(connectionString)) return connectionString;
  161. var sDbHost = DBUnit.GetAppSettings("DbHost");
  162. var sDbPort = DBUnit.GetAppSettings("DbPort");
  163. var sDbName = DBUnit.GetAppSettings("DbName");
  164. var sDbUser = DBUnit.GetAppSettings("DbUser");
  165. var sDbPassword = DBUnit.GetAppSettings("DbPassword");
  166. var sDbMinPoolSize = DBUnit.GetAppSettings("DbMinPoolSize");
  167. var sDbMaxPoolSize = DBUnit.GetAppSettings("DbMaxPoolSize");
  168. var sDbCharset = DBUnit.GetAppSettings("DbCharset");
  169. var sb = new StringBuilder();
  170. sb.Append("Data Source=").Append(sDbHost).Append(";");
  171. if (!String.IsNullOrEmpty(sDbPort))
  172. {
  173. sb.Append("port=").Append(sDbPort).Append(";");
  174. }
  175. sb.Append("User ID=").Append(sDbUser).Append(";");
  176. sb.Append("Password=").Append(sDbPassword).Append(";");
  177. sb.Append("DataBase=").Append(sDbName).Append(";");
  178. if (!String.IsNullOrEmpty(sDbMinPoolSize))
  179. {
  180. sb.Append("Min Pool Size=").Append(sDbMinPoolSize).Append(";");
  181. }
  182. if (!String.IsNullOrEmpty(sDbMinPoolSize))
  183. {
  184. sb.Append("Max Pool Size=").Append(sDbMaxPoolSize).Append(";");
  185. }
  186. if (!String.IsNullOrEmpty(sDbCharset))
  187. {
  188. sb.Append("charset=").Append(sDbCharset).Append(";");
  189. }
  190. return sb.ToString();
  191. }
  192. catch
  193. {
  194. throw new Exception("web.config檔appSettings中資料庫連接字串未配置或配置錯誤,必須為Key=\"connectionString\"");
  195. }
  196. }
  197. public static List<SqlFuncExternal> GetExpMethods(List<SqlFuncExternal> expfn)
  198. {
  199. var saMethods = new List<SqlFuncExternal>();
  200. var method = new SqlFuncExternal
  201. {
  202. UniqueMethodName = nameof(ISNULL),
  203. MethodValue = (expInfo, dbType, expContext) =>
  204. {
  205. return $"ISNULL({expInfo.Args[0].MemberName},'')";
  206. }
  207. };
  208. if (expfn != null && expfn.Count > 0)
  209. {
  210. foreach (var _method in expfn)
  211. {
  212. saMethods.Add(_method);
  213. }
  214. }
  215. return saMethods;
  216. }
  217. public static string ISNULL<T>(T str)
  218. {
  219. throw new NotSupportedException("Can only be used in expressions");
  220. }
  221. }
  222. }