Database abstract factory implemented with C # (2)
The following three classes are Factory for SQL Server private connections, the specific implementation of the OLEDB connection, and ODBC connections:
(3) SQLFActory.cs
Using system; using system.data.sqlclient; using system.configuration;
Namespace DBService {/// /// Dedicated to SQLServer dedicated plants /// summary> public class sqlfactory: Abstractory: AbstractDdbFactory {/// /// Construction function /// summary> public Sqlfactory () {}
/// /// Establish a default Connection object /// summary> /// Connection object returns> public idbconnection createconnection () {return new sqlConnection ();}
/// / / / Establish Connection object /// // // // // connection string param> /// /// Connection object < / returns> public idbconnection createConnection (String strconn) {return new sqlconnection (strconn);}
/// /// Establish a Command object /// / summary> /// Command object returns> public idbcommand createcommand () {return new sqlcommand ();
/// /// Establish DataAdapter object /// summary> /// DataAdapter object returns> public idbdataadapter createDataAdapter () {Return New SqlDataAdapter ()
/// /// According to Connection Transaction /// summary> /// Connection object param> /// Transaction object returns> public IdbTransaction CreateTractions {Return MyDbConnection.begintransAction ();
/// // / Established DataReader /// //////////////////////////////////// DataReader Objects Returns> PUBLIC IDataReader CreateDataReader (idbcommand mydbcommand) {return mydbcommand.executeReader ();} /// /// get connection string /// summary> /// connection string returns> public String GetConnectionString () {string strServer = ConfigurationSettings.AppSettings [ "SqlServerServer"]; string strDatabase = ConfigurationSettings.AppSettings [ "SqlServerDatabase"]; string strUid = ConfigurationSettings.AppSettings [ "SqlServerUid"]; string strPwd = ConfigurationSettings.AppSettings [ "SqlServerPwd" ]; String strconnectionstring = "server =" strserver "; database =" strdatabase "; uid =" struid "; pwd =" strpwd ";" returnnectionstring;}
}
(4) OLEDBFActory.cs
Using system.data; using system.data.oledb; using system.configuration;
Namespace DBService {/// // / /// summary> public class oledbfactory: abstractDbFactory {/// /// Constructor /// summary> public oledbFactory: AbstractDbFactory {/// () {}
/// /// Establish a default Connection object /// summary> /// Connection object returns> public idbconnection createconnection () {return new oledbConnection ();}
/// / / / Establish Connection object /// // // // // connection string param> /// /// Connection object < / returns> public idbconnection createConnection (String strconn) {Return New OledbConnection (StrConn);} /// /// Establishing Command Object /// summary> /// Command Object RETURNS> Public IDBCommand CreateCommand () {return new oledbcommand ();
/// /// Establish DataAdapter object /// summary> /// DataAdapter object returnAplic idbdataadapter createDataAdapter () {Return New OledbDataAdapter ();}
/// /// According to Connection Transaction /// summary> /// Connection object param> /// Transaction object returns> public IdbTransaction CreateTractions {Return MyDbConnection.begintransAction ();
/// // / Established DataReader /// //////////////////////////////////// DataReader Objects Returns> PUBLIC IDataReader CreateDataReader (idbcommand mydbcommand) {return mydbcommand.executeReader ();}
/// /// Get a connection string /// ///// Connection string returns> public string getConnectionstring () {string strprovider = configurationSettings.appsettings ["OLEDBPROVIDER"] ; string strDataSource = ConfigurationSettings.AppSettings [ "OleDbDataSource"]; string strConnectionString = "Provider =" strProvider "; Data Source =" strDataSource ";"; return strConnectionString;}}}
(5) odbcfactory.cs
Using system; using system.data.odbc; using system.configuration;
Namespace dbservice {/// // //// summary> public class odbcfactory: AbstractDbFactory {/// /// summary> Public ODBCFACTory: () {}
/// /// Establish a default Connection object /// summary> /// Connection object returns> public idbconnection createconnection () {returnne odbcConnection ();}
/// / / / Establish Connection object /// // // // // connection string param> /// /// Connection object < / RETURNS> Public IDBConnection CreateConnection (String StrConn) {Return New OdbcConnection (STRCONN);
/// /// Establish Command Object /// Summary> /// Command Object Returns> Public IDBCommand CreateCommand () {Return New OdbcCommand ();
/// /// Establish DataAPter object /// summary> /// DataAdapter object returnapter object returns> public idbdataadapter createDataAdapter () {Return new odbcdataadapter ()
/// /// According to Connection Transaction /// summary> /// Connection object param> /// Transaction object returns> public IDbTransaction CreateTransaction (IDbConnection myDbConnection) {return myDbConnection.BeginTransaction ();} /// /// established DataReader /// summary> /// The Command Command Object PARAM> /// DataReader object returns> public iDataReader createDataReader (idbcommand mydbcommand) {return mydbcommand.executeReader ();}
/// /// Get a connection string /// ////// returns> public string getConnectionstring () {string striver = configurationSettings.appsettings ["odbcdriver"]; string strDbq = ConfigurationSettings.appsettings ["odbcdbq"]; string strconnectionstring = "driver = {" strider "}; dbq =" strdbq ";" returnnectionstring;}
}
to be continued......