asp.net core3.1代码中获取数据库连接字符串
{ "ConnectionStrings": { "Default": "server=192.168.3.5;Initial Catalog=your_db_name;User=root;Password=111222;port=3306;sslMode=None;" } }
public class DapperMysqlHelper { private readonly int commandTimeout = 30;//设置超时时间5秒 private readonly IConfiguration _configuration; /// <summary> /// 构造方法 /// </summary> /// <param name="options"></param> public DapperMysqlHelper(IConfiguration configuration) { _configuration = configuration; } /// <summary> /// 获取数据库连接对象 /// </summary> /// <returns></returns> public MySqlConnection GetConnection() { return new MySqlConnection(_configuration.GetConnectionString("Default")); } }
Dapper的一个辅助类:DapperMysqlHelper,其它的方法不贴了。