C # connecting the Dbprovidername configuration of a common database

Source: Internet
Author: User
Tags sqlite

In order to adapt to the various database switching, we sometimes want to customize to write a generic sqlhelper. The usual practice is in System.Data.Common.DbProviderFactories.GetFactory (Dbprovidername). CreateConnection () to return the connection of the corresponding database.

Directly on the test source:

            stringDbprovidername ="System.Data.Odbc"; Dbprovidername="System.Data.OleDb"; Dbprovidername="System.Data.SqlClient"; Dbprovidername="System.Data.OracleClient"; //Reference System.Data.SQLite.dllDbprovidername ="System.Data.SQLite"; //Reference MySql.Data.dllDbprovidername ="MySql.Data.MySqlClient"; //Reference Oracle.ManagedDataAccess.dllDbprovidername ="Oracle.ManagedDataAccess.Client"; DbProviderFactory dbfactory=dbproviderfactories.getfactory (dbprovidername); DbConnection Dbconn=dbfactory.            CreateConnection (); Dbconn. ConnectionString=string. Empty;//Database connection String

Config configuration to be aware of:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration>  <StartupuseLegacyV2RuntimeActivationPolicy= "true">    <supportedruntimeversion= "v4.0"SKU=". netframework,version=v4.0 "/>    <supportedruntimeversion= "v2.0.50727"/>  </Startup><System.Data>    <dbproviderfactories>      <Removeinvariant= "System.Data.SQLite"/>      <Addname= "SQLite Data Provider"invariant= "System.Data.SQLite"Description= ". Net Framework Data Provider for SQLite"type= "System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />            <Removeinvariant= "MySql.Data.MySqlClient"/>      <Addname= "MySQL Data Provider"invariant= "MySql.Data.MySqlClient"Description= ". Net Framework Data Provider for MySQL"type= "MySql.Data.MySqlClient.MySqlClientFactory, mysql.data" />            <Removeinvariant= "Oracle.ManagedDataAccess.Client"/>      <Addname= "Oracle Data Provider"invariant= "Oracle.ManagedDataAccess.Client"Description= "Oracle Data Provider for. NET"type= "Oracle.ManagedDataAccess.Client.OracleClientFactory, oracle.manageddataaccess" />              </dbproviderfactories>    </System.Data></Configuration>

Finally, attach the code.

C # connecting the Dbprovidername configuration of a common database

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.