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,其它的方法不贴了。

posted @ 2022-10-25 20:08  屌丝大叔的笔记  阅读(101)  评论(0编辑  收藏  举报