Dapper获取连接类
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; using System.Linq; using System.Text; namespace SmartCA { public class DbConnection { public const string Platform = "Platform";
/// <summary> /// 获取数据库连接并打开 /// </summary> /// <param name="connnectionStringName"></param> /// <returns></returns> public static IDbConnection GetDbConnection(string connnectionStringName) { string provider = ConfigurationManager.ConnectionStrings[connnectionStringName].ProviderName; string connectionString = ConfigurationManager.ConnectionStrings[connnectionStringName].ConnectionString; IDbConnection connection; switch (provider) { case "MySql.Data.SqlClient": connection = new SqlConnection(connectionString); break; //case "MySql.Data.MySqlClient": // connection = new MySqlConnection(connectionString); // break; case "System.Data.Odbc": connection = new OleDbConnection(connectionString); break; case "System.Data.OleDb": connection = new OleDbConnection(connectionString); break; default: connection = new SqlConnection(connectionString); break; } if (connection.State == ConnectionState.Closed) connection.Open(); return connection; } } }