Dapper(二) 安装和数据库连接

一.安装

   1. nuget中获取dapper

Dapper(二) 安装和数据库连接_第1张图片

 

二.数据库连接.

     

 public class MysqlHelper
    {

        protected readonly bool IsAppVeyor = Environment.GetEnvironmentVariable("Appveyor")?.ToUpperInvariant() == "TRUE";

        /// 
        /// 主库连接信息
        /// 
        public static string Master_datasource = ConfigurationManager.AppSettings["master_datesource"];
        /// 
        /// 从库连接信息
        /// 
        public static string Save_datasource = ConfigurationManager.AppSettings["save_datesource"];

        /// 
        /// 封装对Mysql的连接
        /// 
        /// 是否打开连接
        /// 
        /// 
        /// 返回数据库连接
        public MySql.Data.MySqlClient.MySqlConnection GetMySqlConnection(bool open = true, bool convertZeroDatetime = false, bool allowZeroDatetime = false)
        {
            string cs = IsAppVeyor ? Master_datasource : Save_datasource;
            var csb = new MySql.Data.MySqlClient.MySqlConnectionStringBuilder(cs)
            {
                AllowZeroDateTime = allowZeroDatetime,
                ConvertZeroDateTime = convertZeroDatetime
            };
            var conn = new MySql.Data.MySqlClient.MySqlConnection(csb.ConnectionString);
            if (open) conn.Open();
            return conn;
        }
    }

配置文件


    
    
    

 

你可能感兴趣的:(SQL,Dapper基本应用)