I found a factory-type Database Help class in my blog some time ago. Which of the following experts can't remember clearly? Mark it now.
1. Database Type Enumeration
Public EnumDbprovidertype:Byte{Sqlserver, MySQL, SQLite, Oracle, ODBC, oledb, Firebird, PostgreSQL, DB2, Informix, sqlserverce}
2. dbproviderfactory factory class
Public Class Providerfactory { Private Static Dictionary <dbprovidertype, String > Providerinvariantnames =New Dictionary <dbprovidertype, String > (); Private Static Dictionary <dbprovidertype, dbproviderfactory> providerfactoies = New Dictionary <dbprovidertype, dbproviderfactory> ( 20 ); Static Providerfactory () {providerinvariantnames. Add (dbprovidertype. sqlserver, " System. Data. sqlclient " ); Providerinvariantnames. Add (dbprovidertype. oledb, " System. Data. oledb " ); Providerinvariantnames. Add (dbprovidertype. ODBC, " System. Data. ODBC " ); Providerinvariantnames. Add (dbprovidertype. Oracle, " Oracle. dataaccess. Client " ); Providerinvariantnames. Add (dbprovidertype. MySQL, " MySQL. Data. mysqlclient " ); Providerinvariantnames. Add (dbprovidertype. SQLite, " System. Data. SQLite " ); Providerinvariantnames. Add (dbprovidertype. Firebird, " Firebirdsql. Data. Firebird " ); Providerinvariantnames. Add (dbprovidertype. PostgreSQL, " Npgsql " ); Providerinvariantnames. Add (dbprovidertype. DB2, " IBM. Data. db2.iseries " ); Providerinvariantnames. Add (dbprovidertype. Informix, " IBM. Data. Informix " ); Providerinvariantnames. Add (dbprovidertype. sqlserverce, " System. Data. sqlserverce " );} Public Static String Getproviderinvariantname (dbprovidertype providertype ){ Return Providerinvariantnames [providertype];} Public Static Dbproviderfactory getdbproviderfactory (dbprovidertype providertype ){ If (!Providerfactoies. containskey (providertype) {providerfactoies. Add (providertype, importdbproviderfactory (providertype ));} Return Providerfactoies [providertype];} Private Static Dbproviderfactory importdbproviderfactory (dbprovidertype providertype ){ String Providername = Providerinvariantnames [providertype]; dbproviderfactory Factory = Null ; Try {Factory = Dbproviderfactories. getfactory (providername );} Catch (Argumentexception e) {Factory = Null ;} Return Factory ;}}
3. database operation help
Public Sealed Class Dbhelper { Public String Connectionstring { Get ; Set ;} Private Dbproviderfactory providerfactory; Public Dbhelper ( String Connectionstring, dbprovidertype providertype) {connectionstring = Connectionstring; providerfactory =Providerfactory. getdbproviderfactory (providertype ); If (Providerfactory = Null ){ Throw New Argumentexception ( " Can't load dbproviderfactory for given value of providertype " );}} * ********* The specific ADO. Net Operation Method is slightly ************ Public Int Executenonquery (String SQL, ilist <dbparameter> Parameters) Public Dbdatareader executereader ( String SQL, ilist <dbparameter> Parameters )}
4. Database Configuration File
<Connectionstrings> <Add name ="Darjuan_ DB"Connectionstring ="Data Source = DARJUAN-PC; database = darjuan_db; user id = ***; Password = ***; Connection Reset = false; timeout = 120; connection lifetime = 120; min pool size = 20; Max pool size = 50"Providername ="System. Data. sqlclient"/> </Connectionstrings>
<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.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 = 89845dcd80cc91 " /> <Add name = " SQLite data provider " Invariant = " System. Data. SQLite " Description =" . NET Framework data provider for SQLite " Type = " System. Data. SQLite. sqlitefactory, system. Data. SQLite " /> <Add name = " Informix Data Provider " Invariant = " IBM. Data. Informix " Description = " . NET Framework data provider for Informix " Type = " IBM. Data. Informix. ifxfactory, IBM. Data. Informix " /> <Add name = " DB2 data provider " Invariant = " IBM. Data. db2.iseries " Description = " . NET Framework data provider for DB2 iseries " Type = " IBM. Data. db2.iseries. db2factory, IBM. Data. db2.iseries " /> <Add name = " Firebird data provider " Invariant = " Firebirdsql. Data. firebirdclient " Description = " Firebird " Type = " Firebirdsql. Data. firebirdclient. firebirdclientfactory, firebirdsql. Data. firebirdclient " /> <Add name = " Oracle Data Provider " Invariant = " Oracle. dataaccess. Client " Description = " . NET Framework data provider for Oracle " Type = " Oracle. dataaccess. Client. oracleclientfactory, Oracle. dataaccess " /> <Add name = " PostgreSQL data provider " Invariant = " Npgsql " Description = " . NET Framework data provider for PostgreSQL " Type = " Npgsql. npgsqlfactory, system. Data " /> </Dbproviderfactories> </system. Data>