本文主要是介绍SqlSugar链接数据库 读取appsetting中配置的链接字符串,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
首先建一个类
using SqlSugar;namespace PayManagerCore.Filter
{public class SqlSugarHelper{private static readonly Lazy<SqlSugarClient> _db = new Lazy<SqlSugarClient>(() =>{IConfigurationRoot configuration = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory).AddJsonFile("appsettings.json").Build();string connectionString = configuration.GetConnectionString("sqlcon");var db = new SqlSugarClient(new ConnectionConfig(){ConnectionString = connectionString,DbType = DbType.MySql, // 根据实际情况修改数据库类型InitKeyType = InitKeyType.Attribute,IsAutoCloseConnection = true});return db;});public static SqlSugarClient DB => _db.Value;}
}
然后 appsetting.json中配置你的链接字符串
{"Logging": {"LogLevel": {"Default": "Information","Microsoft.AspNetCore": "Warning"}},"AllowedHosts": "*","ConnectionStrings": {"sqlcon": "server=.;uid=123;pwd=123;database=mysql01",}
}
然后控制器中这样调用
:SqlSugarHelper.DB
if (SqlSugarHelper.DB.Queryable<UserInfo>().First(it => it.name == username) == null){ViewBag.notice = "用户名不存在";return View();}
搞定
这篇关于SqlSugar链接数据库 读取appsetting中配置的链接字符串的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!