当前位置:首页 > 经验

详解provider配置 providername是什么意思

为了适应应对各种数据库间切换,我们有时想要要自定义写一个通用的SqlHelper。常用的做法就是在
System.Data.Common.DbProviderFactories.GetFactory(dbProviderName).CreateConnection ,来返回对应的 数据库的Connection 。

常用数据库的DBProviderName配置

直接上测试源码:

 string dbProviderName = "System.Data.Odbc";
 dbProviderName = "System.Data.OleDb";
 dbProviderName = "System.Data.SqlClient";
 dbProviderName = "System.Data.OracleClient";

 //引用 System.Data.SQLite.dll
 dbProviderName = "System.Data.SQLite";

 //引用 MySql.Data.dll
 dbProviderName = "MySql.Data.MySqlClient";

 //引用 Oracle.ManagedDataAccess.dll
 dbProviderName = "Oracle.ManagedDataAccess.Client";


 DbProviderFactory dbfactory = DbProviderFactories.GetFactory(dbProviderName);
 DbConnection dbconn = dbfactory.CreateConnection;

 dbconn.ConnectionString = string.Empty;// 数据库连接字符串

需要注意的 config 配置:

<#xml version="1.0" encoding="utf-8"#>
<configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
<system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite"/>
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
      
      <remove invariant="MySql.Data.MySqlClient"/>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data" />
      
      <remove invariant="Oracle.ManagedDataAccess.Client"/>
      <add name="Oracle Data Provider" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET" type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess" />    
      
    </DbProviderFactories>
  
  </system.data>
</configuration>

最后附上代码。

声明:此文信息来源于网络,登载此文只为提供信息参考,并不用于任何商业目的。如有侵权,请及时联系我们:fendou3451@163.com
标签:

  • 关注微信

相关文章