using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DbHelper { public class DbContext { private static ConnectionConfig? _config = null; public static void SetConfig(ConnectionConfig value) { _config = value; } /// /// 用单例模式 /// private static readonly SqlSugarScope SqlSugarScope = new SqlSugarScope(new ConnectionConfig() { ConnectionString = _config!.ConnectionString, //连接符字串 DbType = _config.DbType, //数据库类型 IsAutoCloseConnection = true //不设成true要手动close }, db => { //(A)全局生效配置点 //调试SQL事件,可以删掉 db.Aop.OnLogExecuting = (sql, pars) => { DbLog.DBEX(sql); //输出sql,查看执行sql //5.0.8.2 获取无参数化 SQL //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars) }; }); /// /// 获取db链接 /// /// /// public static SqlSugarScope Db { get { if (_config == null) throw new Exception("请使用SetConfig方法写入数据库链接信息"); return SqlSugarScope; } } } }