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