C # DBProviderName configuration for connecting to common databases,

Source: Internet
Author: User

C # DBProviderName configuration for connecting to common databases,

To adapt to various database switching, we sometimes want to customize a general SqlHelper. The Common method is to return the Connection of the corresponding database in System. Data. Common. DbProviderFactories. GetFactory (dbProviderName). CreateConnection.

Directly test the source code:

String dbProviderName = "System. data. odbc "; dbProviderName =" System. data. oleDb "; dbProviderName =" System. data. sqlClient "; dbProviderName =" System. data. oracleClient "; // reference System. data. SQLite. dll dbProviderName = "System. data. SQLite "; // reference MySql. data. dll dbProviderName = "MySql. data. mySqlClient "; // reference Oracle. managedDataAccess. dll dbProviderName = "Oracle. managedDataAccess. client "; DbProviderFactory dbfactory = DbProviderFactories. getFactory (dbProviderName); DbConnection dbconn = dbfactory. createConnection (); dbconn. connectionString = string. empty; // database connection string

Config Configuration:

<?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>

 

The code is attached.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.