Database abstract factory implemented with C # (2)

zhaozj2021-02-16  49

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 /// public class sqlfactory: Abstractory: AbstractDdbFactory {/// /// Construction function /// public Sqlfactory () {}

///

/// Establish a default Connection object /// /// Connection object public idbconnection createconnection () {return new sqlConnection ();}

///

/ / / Establish Connection object /// connection string /// /// Connection object < / returns> public idbconnection createConnection (String strconn) {return new sqlconnection (strconn);}

///

/// Establish a Command object /// /// Command object public idbcommand createcommand () {return new sqlcommand ();

///

/// Establish DataAdapter object /// /// DataAdapter object public idbdataadapter createDataAdapter () {Return New SqlDataAdapter ()

///

/// According to Connection Transaction /// /// Connection object /// Transaction object public IdbTransaction CreateTractions {Return MyDbConnection.begintransAction ();

///

// / Established DataReader /// DataReader Objects PUBLIC IDataReader CreateDataReader (idbcommand mydbcommand) {return mydbcommand.executeReader ();} /// /// get connection string /// /// connection string 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 {///

// / /// public class oledbfactory: abstractDbFactory {/// /// Constructor /// public oledbFactory: AbstractDbFactory {/// () {}

///

/// Establish a default Connection object /// /// Connection object public idbconnection createconnection () {return new oledbConnection ();}

///

/ / / Establish Connection object /// connection string /// /// Connection object < / returns> public idbconnection createConnection (String strconn) {Return New OledbConnection (StrConn);} /// /// Establishing Command Object /// /// Command Object Public IDBCommand CreateCommand () {return new oledbcommand ();

///

/// Establish DataAdapter object /// /// DataAdapter object

///

/// According to Connection Transaction /// /// Connection object /// Transaction object public IdbTransaction CreateTractions {Return MyDbConnection.begintransAction ();

///

// / Established DataReader /// DataReader Objects PUBLIC IDataReader CreateDataReader (idbcommand mydbcommand) {return mydbcommand.executeReader ();}

///

/// Get a connection string /// Connection string 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 {///

// //// public class odbcfactory: AbstractDbFactory {/// /// Public ODBCFACTory: () {}

///

/// Establish a default Connection object /// /// Connection object public idbconnection createconnection () {returnne odbcConnection ();}

///

/ / / Establish Connection object /// connection string /// /// Connection object < / RETURNS> Public IDBConnection CreateConnection (String StrConn) {Return New OdbcConnection (STRCONN);

///

/// Establish Command Object /// /// Command Object Public IDBCommand CreateCommand () {Return New OdbcCommand ();

///

/// Establish DataAPter object /// /// DataAdapter object public idbdataadapter createDataAdapter () {Return new odbcdataadapter ()

///

/// According to Connection Transaction /// /// Connection object /// Transaction object public IDbTransaction CreateTransaction (IDbConnection myDbConnection) {return myDbConnection.BeginTransaction ();} /// /// established DataReader /// /// The Command Command Object /// DataReader object public iDataReader createDataReader (idbcommand mydbcommand) {return mydbcommand.executeReader ();}

///

/// Get a connection string /// public string getConnectionstring () {string striver = configurationSettings.appsettings ["odbcdriver"]; string strDbq = ConfigurationSettings.appsettings ["odbcdbq"]; string strconnectionstring = "driver = {" strider "}; dbq =" strdbq ";" returnnectionstring;}

}

to be continued......

转载请注明原文地址:https://www.9cbs.com/read-24824.html

New Post(0)