123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- 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;
- }
- /// <summary>
- /// 用单例模式
- /// </summary>
- 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)
- };
- });
- /// <summary>
- /// 获取db链接
- /// </summary>
- /// <returns></returns>
- /// <exception cref="Exception"></exception>
- public static SqlSugarScope Db
- {
- get
- {
- if (_config == null) throw new Exception("请使用SetConfig方法写入数据库链接信息");
- return SqlSugarScope;
- }
- }
- }
- }
|