一.安装
1. nuget中获取dapper
二.数据库连接.
public class MysqlHelper
{
protected readonly bool IsAppVeyor = Environment.GetEnvironmentVariable("Appveyor")?.ToUpperInvariant() == "TRUE";
/// <summary>
/// 主库连接信息
/// </summary>
public static string Master_datasource = ConfigurationManager.AppSettings["master_datesource"];
/// <summary>
/// 从库连接信息
/// </summary>
public static string Save_datasource = ConfigurationManager.AppSettings["save_datesource"];
/// <summary>
/// 封装对Mysql的连接
/// </summary>
/// <param name="open">是否打开连接</param>
/// <param name="convertZeroDatetime"></param>
/// <param name="allowZeroDatetime"></param>
/// <returns>返回数据库连接</returns>
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;
}
}
配置文件
<!-- mysql Start -->
<add key="master_datesource" value="Server=地址;Database=数据库;Uid=账号;Pwd=密码;SslMode = none;" />
<add key="save_datesource" value="Server=地址;Database=数据库;Uid=账号;Pwd=密码;SslMode = none;"/>
<!-- mysql End -->