使用多态来实现数据库之间的切换
1、一般数据库之间的转换时这样实现的
static void Main(string[] args) { //必须引入System.Data程序集 string connectionString = ConfigurationManager.ConnectionStrings["SqlConfigurationManager"].ConnectionString; string providerName = ConfigurationManager.ConnectionStrings["SqlConfigurationManager"].ProviderName; IDbConnection con; if (providerName == "Access") { con = new OleDbConnection(connectionString); } else if (providerName == "Sql") { con = new SqlConnection(connectionString); } else { throw new Exception("不存在!"); } using (con) { con.Open(); using (IDbCommand cmd = con.CreateCommand()) { cmd.CommandText = "insert into Person (age) values(@age) ";//Access占位符通常使用问号?,当多个占位符参数时,要注意个数,而如果使用的是@作为占位符的话,就必须要注意Command添加参数时的顺序,不然报错,而?问号不要在意这个顺序,所以推荐使用问号?作为占位符 IDbDataParameter parameter = cmd.CreateParameter(); parameter.ParameterName = "@age"; parameter.DbType = DbType.Int32; parameter.Value = "110"; cmd.Parameters.Add(parameter); cmd.ExecuteNonQuery(); } } Console.WriteLine("插入成功!"); Console.ReadKey(); }
App.config配置如下
<configuration> <connectionStrings> <add name="SqlConfigurationManager" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\db1.mdb" providerName="Access"/> <!--<add name="SqlConfigurationManager" connectionString="Server=.;Database=Person;Integrated Security=True;Pooling=False" providerName="MsSqlServer"/>--> </connectionStrings> </configuration>
2、通过DbProviderFactories实现
一般providerName是有规范的命名的
在C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config里面的system.data节点下定义了
<system.data> <DbProviderFactories> <add name="Odbc Data Provider" invariant="System.Data.Odbc" description=".Net Framework Data Provider for Odbc" type="System.Data.Odbc.OdbcFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <add name="OleDb Data Provider" invariant="System.Data.OleDb" description=".Net Framework Data Provider for OleDb" type="System.Data.OleDb.OleDbFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <add name="OracleClient Data Provider" invariant="System.Data.OracleClient" description=".Net Framework Data Provider for Oracle" type="System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.3.5" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/> <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/> </DbProviderFactories> </system.data>
如果providerName为System.Data.Odbc,那么可以用于所有数据库配置
如果为System.Data.OleDb,可用于Access数据库配置
如果为System.Data.OracleClient,可用于Oracle数据库配置
如果为System.Data.SqlClient,可用于MsSqlServer数据库配置
static void Main(string[] args) { string connectionString = ConfigurationManager.ConnectionStrings["SqlConfigurationManager"].ConnectionString; string providerName = ConfigurationManager.ConnectionStrings["SqlConfigurationManager"].ProviderName; //必须引入System.Data程序集 DbProviderFactory factory = DbProviderFactories.GetFactory(providerName); using (DbConnection con = factory.CreateConnection()) { con.ConnectionString = connectionString; con.Open(); using (DbCommand cmd = factory.CreateCommand()) { cmd.CommandText = "insert into Person (age) values(@age)"; cmd.Connection = con; DbParameter parameter = cmd.CreateParameter(); parameter.ParameterName = "@age";//Access不用此占位符(?)也能顺利插入 parameter.DbType = DbType.Int32; parameter.Value = "1010"; cmd.Parameters.Add(parameter); cmd.ExecuteNonQuery(); } } Console.WriteLine("插入成功!"); Console.ReadKey(); }
注意ProviderName的配置
<configuration> <connectionStrings> <add name="SqlConfigurationManager" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\db1.mdb" providerName="System.Data.OleDb"/> <!--<add name="SqlConfigurationManager" connectionString="Server=.;Database=Person;Integrated Security=True;Pooling=False" providerName="System.Data.SqlClient"/>--> </connectionStrings> </configuration>