using SqlSugar; using System; using System.Data; using System.Threading; using WCS.Data.Utils; namespace WCS.Data { /// /// Sugar ORM父类, 封装一些基本的操作 /// public class SugarBase { public static SqlSugarClient DB { get { return InitDB(30, SqlSugar.DbType.SqlServer, true); } } /// /// 数据库连接字符串, 在配置文件中的connectionStrings节点中添加name为SqlSugar的节点信息即可, 会自动获取 /// public static string DBConnectionString { private get; set; } /// /// 获得SqlSugarClient(使用该方法, 默认请手动释放资源, 如using(var db = SugarBase.GetIntance()){你的代码}, 如果把isAutoCloseConnection参数设置为true, 则无需手动释放, 会每次操作数据库释放一次, 可能会影响性能, 请自行判断使用) /// /// 等待超时时间, 默认为30秒 (单位: 秒) /// 数据库类型, 默认为SQL Server /// 是否自动关闭数据库连接, 默认不是, 如果设置为true, 则会在每次操作完数据库后, 即时关闭, 如果一个方法里面多次操作了数据库, 建议保持为false, 否则可能会引发性能问题 /// /// 旷丽文 public static SqlSugarClient GetIntance(int commandTimeOut = 30, SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer, bool isAutoCloseConnection = false) { return InitDB(commandTimeOut, dbType, isAutoCloseConnection); } /// /// 初始化ORM连接对象, 一般无需调用, 除非要自己写很复杂的数据库逻辑 /// /// 等待超时时间, 默认为30秒 (单位: 秒) /// 数据库类型, 默认为SQL Server /// 是否自动关闭数据库连接, 默认不是, 如果设置为true, 则会在每次操作完数据库后, 即时关闭, 如果一个方法里面多次操作了数据库, 建议保持为false, 否则可能会引发性能问题 /// 旷丽文 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; } /// /// 执行数据库操作 /// /// 返回值类型 /// 方法体 /// /// 旷丽文 public static Result Exec(Func 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(); } } } /// /// 带事务处理的执行数据库操作 /// /// 返回值类型 /// 方法体 /// /// 旷丽文 public static Result ExecTran(Func 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; } /// /// 无事务无返回值的通用代码 /// /// /// public static string TryExecute(Action 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; } /// /// 无事务的有返回值通用代码 /// /// /// public static string TryFuncExecute(Func 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 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(); } } /// /// 有事务的无返回值通用代码 /// /// /// public static string TryTranExecute(Action 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; } /// /// 有事务的无返回值通用代码 /// /// /// public static string TryTranExecute(Action 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; } /// /// 有事务的有返回值通用代码 /// /// /// public static object TryTranFuncExecute(Func 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 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(); } } } }