using Microsoft.Extensions.Configuration; using Microsoft.IdentityModel.Protocols; using SqlSugar; using System; using System.Collections.Generic; using System.Configuration; using System.IO; namespace PT.DAL { public class SqlSugarHelper { //多库情况下使用说明: //如果是固定多库可以传 new SqlSugarScope(List<ConnectionConfig>,db=>{}) 文档:多租户 //如果是不固定多库 可以看文档Saas分库 //static string connstr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; public readonly static IConfiguration Configuration; static SqlSugarHelper() { //Install-Package Microsoft.Extensions.Configuration //Install - Package Microsoft.Extensions.Configuration.Json Configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", optional: true) .Build(); } public static string conn { get { return Configuration.GetConnectionString("BaseDb"); } } //用单例模式 public static SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig() { ConnectionString = "server=172.16.73.252;uid=sa;pwd=sa123456789;database=IpManDB",//连接符字串 conn 明明有值 此处却取不到值 不知为什么? DbType = DbType.SqlServer,//数据库类型 IsAutoCloseConnection = true //不设成true要手动close }, db => { //(A)全局生效配置点,一般AOP和程序启动的配置扔这里面 ,所有上下文生效 //调试SQL事件,可以删掉 db.Aop.OnLogExecuting = (sql, pars) => { Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响 //获取原生SQL推荐 5.1.4.63 性能OK //UtilMethods.GetNativeSql(sql, pars); //获取无参数化SQL 对性能有影响,特别大的SQL参数多的,调试使用 //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars) }; //多个配置就写下面 //db.Ado.IsDisableMasterSlaveSeparation=true; //注意多租户 有几个设置几个 //db.GetConnection(i).Aop }); } }
标签:SqlSugarHelper,db,static,sql,cs,using,Configuration From: https://www.cnblogs.com/zyx321/p/17370382.html