123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- using SqlSugar;
- using System;
- using System.Data;
- using System.Threading;
- using WCS.Data.Utils;
- namespace WCS.Data
- {
- /// <summary>
- /// Sugar ORM父类, 封装一些基本的操作
- /// </summary>
- public class SugarBase
- {
- public static SqlSugarClient DB
- {
- get
- {
- return InitDB(30, SqlSugar.DbType.SqlServer, true);
- }
- }
- /// <summary>
- /// 数据库连接字符串, 在配置文件中的connectionStrings节点中添加name为SqlSugar的节点信息即可, 会自动获取
- /// </summary>
- public static string DBConnectionString { private get; set; }
- /// <summary>
- /// 获得SqlSugarClient(使用该方法, 默认请手动释放资源, 如using(var db = SugarBase.GetIntance()){你的代码}, 如果把isAutoCloseConnection参数设置为true, 则无需手动释放, 会每次操作数据库释放一次, 可能会影响性能, 请自行判断使用)
- /// </summary>
- /// <param name="commandTimeOut">等待超时时间, 默认为30秒 (单位: 秒)</param>
- /// <param name="dbType">数据库类型, 默认为SQL Server</param>
- /// <param name="isAutoCloseConnection">是否自动关闭数据库连接, 默认不是, 如果设置为true, 则会在每次操作完数据库后, 即时关闭, 如果一个方法里面多次操作了数据库, 建议保持为false, 否则可能会引发性能问题</param>
- /// <returns></returns>
- /// <author>旷丽文</author>
- public static SqlSugarClient GetIntance(int commandTimeOut = 30, SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer, bool isAutoCloseConnection = false)
- {
- return InitDB(commandTimeOut, dbType, isAutoCloseConnection);
- }
- /// <summary>
- /// 初始化ORM连接对象, 一般无需调用, 除非要自己写很复杂的数据库逻辑
- /// </summary>
- /// <param name="commandTimeOut">等待超时时间, 默认为30秒 (单位: 秒)</param>
- /// <param name="dbType">数据库类型, 默认为SQL Server</param>
- /// <param name="isAutoCloseConnection">是否自动关闭数据库连接, 默认不是, 如果设置为true, 则会在每次操作完数据库后, 即时关闭, 如果一个方法里面多次操作了数据库, 建议保持为false, 否则可能会引发性能问题</param>
- /// <author>旷丽文</author>
- private static SqlSugarClient InitDB(int commandTimeOut = 30, SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer, bool isAutoCloseConnection = false)
- {
- var db = new SqlSugarClient(new ConnectionConfig()
- {
- ConnectionString = DBConnectionString,
- DbType = dbType,
- InitKeyType = InitKeyType.Attribute,
- IsAutoCloseConnection = isAutoCloseConnection
- });
- db.Ado.CommandTimeOut = commandTimeOut;
- db.Aop.OnLogExecuting = (sql, pars) =>
- {
- string result = sql + "\r\n";// + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value));
- //if (result.ToLower().Contains("wcs_task"))
- //{
- // Current.Logger_Info.InfoFormat(result + ":" + resultParam);
- //}
- };
- return db;
- }
- /// <summary>
- /// 执行数据库操作
- /// </summary>
- /// <typeparam name="Result">返回值类型</typeparam>
- /// <param name="func">方法体</param>
- /// <returns></returns>
- /// <author>旷丽文</author>
- public static Result Exec<Result>(Func<SqlSugarClient, Result> func, int commandTimeOut = 30, SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer)
- {
- if (func == null) throw new Exception("委托为null, 事务处理无意义");
- using (var db = InitDB(commandTimeOut, dbType))
- {
- try
- {
- return func(db);
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- db.Dispose();
- }
- }
- }
- /// <summary>
- /// 带事务处理的执行数据库操作
- /// </summary>
- /// <typeparam name="Result">返回值类型</typeparam>
- /// <param name="func">方法体</param>
- /// <returns></returns>
- /// <author>旷丽文</author>
- public static Result ExecTran<Result>(Func<SqlSugarClient, Result> func, int commandTimeOut = 30, SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer)
- {
- if (func == null) throw new Exception("委托为null, 事务处理无意义");
- using (var db = InitDB(commandTimeOut, dbType))
- {
- try
- {
- db.Ado.BeginTran(IsolationLevel.Unspecified);
- var result = func(db);
- db.Ado.CommitTran();
- return result;
- }
- catch (Exception ex)
- {
- db.Ado.RollbackTran();
- throw ex;
- }
- finally
- {
- db.Dispose();
- }
- }
- }
- }
- public class TryCachHelper
- {
- public static void TryExecute(Action action)
- {
- try
- {
- action();
- }
- catch (Exception ex)
- {
- LogMessageHelper.RecordLogMessage(ex);
- }
- finally
- {
- }
- }
- public static string TryExecute2(Action action)
- {
- string result = string.Empty;
- try
- {
- action();
- }
- catch (Exception ex)
- {
- result = ex.Message;
- LogMessageHelper.RecordLogMessage(ex);
- }
- finally
- {
- }
- return result;
- }
- /// <summary>
- /// 无事务无返回值的通用代码
- /// </summary>
- /// <param name="action"></param>
- /// <returns></returns>
- public static string TryExecute(Action<SqlSugarClient> action)
- {
- string msg = string.Empty;
- SqlSugarClient db = null;
- try
- {
- db = SugarBase.GetIntance();
- action(db);
- }
- catch (Exception ex)
- {
- msg = ex.Message;
- LogMessageHelper.RecordLogMessage(ex);
- }
- finally
- {
- if (db != null) ((IDisposable)db).Dispose();
- }
- return msg;
- }
- /// <summary>
- /// 无事务的有返回值通用代码
- /// </summary>
- /// <param name="func"></param>
- /// <returns></returns>
- public static string TryFuncExecute(Func<SqlSugarClient, string> func)
- {
- string msg = string.Empty;
- SqlSugarClient db = null;
- try
- {
- db = SugarBase.GetIntance();
- msg = func(db);
- }
- catch (Exception ex)
- {
- msg = ex.Message;
- LogMessageHelper.RecordLogMessage(ex);
- }
- finally
- {
- if (db != null) ((IDisposable)db).Dispose();
- }
- return msg;
- }
- public static void TryExecute(Action<SqlSugarClient> action, string methodName)
- {
- SqlSugarClient db = null;
- try
- {
- db = SugarBase.GetIntance();
- action(db);
- }
- catch (Exception ex)
- {
- LogMessageHelper.RecordLogMessage(ex);
- }
- finally
- {
- if (db != null) ((IDisposable)db).Dispose();
- }
- }
- /// <summary>
- /// 有事务的无返回值通用代码
- /// </summary>
- /// <param name="action"></param>
- /// <returns></returns>
- public static string TryTranExecute(Action<SqlSugarClient> action)
- {
- string msg = string.Empty;
- SqlSugarClient db = null;
- try
- {
- db = SugarBase.GetIntance();
- db.Ado.BeginTran();
- action(db);
- db.Ado.CommitTran();
- }
- catch (Exception ex)
- {
- msg = ex.Message;
- if (db != null) db.Ado.RollbackTran();
- LogMessageHelper.RecordLogMessage(ex);
- }
- finally
- {
- if (db != null) ((IDisposable)db).Dispose();
- }
- return msg;
- }
- /// <summary>
- /// 有事务的无返回值通用代码
- /// </summary>
- /// <param name="action"></param>
- /// <returns></returns>
- public static string TryTranExecute(Action<SqlSugarClient> action, int loc)
- {
- string msg = string.Empty;
- if (Interlocked.Exchange(ref loc, 1) == 0)
- {
- SqlSugarClient db = null;
- try
- {
- db = SugarBase.GetIntance();
- db.Ado.BeginTran();
- action(db);
- db.Ado.CommitTran();
- }
- catch (Exception ex)
- {
- msg = ex.Message;
- if (db != null) db.Ado.RollbackTran();
- LogMessageHelper.RecordLogMessage(ex);
- }
- finally
- {
- if (db != null) ((IDisposable)db).Dispose();
- Interlocked.Exchange(ref loc, 0);
- }
- }
- return msg;
- }
- /// <summary>
- /// 有事务的有返回值通用代码
- /// </summary>
- /// <param name="action"></param>
- /// <returns></returns>
- public static object TryTranFuncExecute(Func<SqlSugarClient, object> func)
- {
- object obj = null;
- SqlSugarClient db = null;
- try
- {
- db = SugarBase.GetIntance();
- db.Ado.BeginTran();
- obj = func(db);
- db.Ado.CommitTran();
- }
- catch (Exception ex)
- {
- obj = null;
- if (db != null) db.Ado.RollbackTran();
- LogMessageHelper.RecordLogMessage(ex);
- }
- finally
- {
- if (db != null) ((IDisposable)db).Dispose();
- }
- return obj;
- }
- public static void TryTranExecute(Action<SqlSugarClient> action, string methodName)
- {
- SqlSugarClient db = null;
- try
- {
- db = SugarBase.GetIntance();
- db.Ado.BeginTran();
- action(db);
- db.Ado.CommitTran();
- }
- catch (Exception ex)
- {
- if (db != null) db.Ado.RollbackTran();
- LogMessageHelper.RecordLogMessage(ex);
- }
- finally
- {
- if (db != null) ((IDisposable)db).Dispose();
- }
- }
- }
- }
|