1 在项目的appsettings.json 文件中,添加 数据库链接字符串
"ConnectionStrings": {"DefaultConnection": "Server=.;Database=Databasename;User Id=sa;Password=123456;Encrypt=false;TrustServerCertificate=true; "
},
注意 Encrypt=false;TrustServerCertificate=true;这个是必须的
2 在dal层 读取 数据库链接字符串
private readonly string connectionString;public SQLBase()
{var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);IConfiguration configuration = builder.Build();connectionString = configuration.GetConnectionString("DefaultConnection") ?? "";
}
connectionString是数据库 链接字符串,可以进行数据连接操作了。