Database abstract factory implemented with C # (all)

xiaoxiao2021-03-06  62

I have recently learned the design model, and I tried to use the C # to use an abstract factory mode to implement the abstract factory. There is slightly different from the standard mode. I have added some my thoughts. I hope everyone criticizes the correct, the code is divided into 6. Category: (1) AbstractDbFactory.cs Using System; Using System.Data; Namespace DBService {///

/// Database Abstract Factory interface /// public interface abstractdbFactory {/// /// Establish a default connection /// /// Database connection idbconnection createConnection (); /// / / / /> Creating the connection string /// < / summary> /// connection string /// connection object iDBConnection CreateConnection (String StrConn); /// /// Establishment Command object /// /// Command object idbcommand createcommand (); /// /// Establish DataAdapter object /// /// DataAdapter object idbdataadapter createDataAdapter (); /// // / according to Connection TRANSAction /// /// Connection object /// Transaction object idbtransaction createTransaction (idbconnection mydbconnection); /// // // based on Command DataReader / // ///

ODBC ": {AbstractDbFactory = New OdbcFactory (); break;}} Return AbstractDbFactory;}}} The following three classes are Factory for SQL Server dedicated connections, OLEDB connections, and ODBC connections: (3) SQLFactory.cs Using System; using System.Data; using System.Data.SqlClient; using System.Configuration; namespace DbService {///

/// /// SqlServer plant for dedicated connection public class SqlFactory: AbstractDbFactory { /// /// Constructor /// public sqlfactory () {} /// /// Establish default Connection object /// /// Connection object public idbconnection createConnection () {return new summary> // / Establish Connection object /// Connection Object /// Transaction Object public IDbTransaction CreateTransaction (IDbConnection myDbConnection) {Return mydbconnection.begintransaction ();} /// /// Established DataReader /// Command object //// < Returns> DataReader object public iDataReader createDataReader (idbcommand mydbcommand) {return mydbcommand.executeReader ();} /// /// get connection string /// /// The connection string public string GetConnectionString () {string strServer = ConfigurationSettings.AppSettings [ "SqlServerServer"]; string strDatabase = ConfigurationSettings.AppSettings [ "SqlServerDatabase"]; string strUid = ConfigurationSettings.AppSettings [ "Sql ServerUid "]; string strPwd = ConfigurationSettings.AppSettings [" SqlServerPwd "]; string strConnectionString =" Server = " strServer "; Database = " strDatabase "; Uid = " strUid "; Pwd = " strPwd ";";}}} (4) OLEDBFACTORY.CS USING SYSTEM; use system.data; use system.data.oledb; use system.configuration; namespace dbservice {/// //> For OLEDB Connected factory ///

Public class oledbfactory: AbstractDbFactory {///

/// Constructor /// public oledbFactory () {} /// /// Establish default Connection object /// /// Connection object public idbconnection createConnection () {Return new oledbconnection ();} /// / / / / / Connection string /// Connection object public idbconnection createconnection (string strconn) {return new oledbconnection (StrConn);} /// /// Establish a Command object /// Command object public idbcommand createcommand () {return new oledbcommand ();} /// /// Establish DataAPter object /// /// DataAdapter object pu BliciDbdataadapter createDataAdapter () {return new oledbdataadapter ();} /// ///> According to Connection Transaction /// ///

myDbCommand "> Command Object /// DataReader Object public IDataReader CreateDataReader (IDbCommand myDbCommand) {return myDbCommand.ExecuteReader ();} ///

/// connection string obtained /// /// 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; using System.Data.Odbc; Using system.configuration; namespace dbservice {/// // / /// public class odbcfactory: AbstractDracfbFactory {//// /// Since / summary> public odbcf Actory () {} /// /// Establish a default Connection object /// /// Connection object public idbconnection createconnection () {return new odbcConnection ();} /// / / / Establish Connection object /// connection string /// /// Connection object < / RETURns> Public IDBConnection CreateConnection (String StrConn) {Return New OdbcConnection (STRCONN);

} ///

/// Establish a Command object /// /// Command object public idbcommand createcommand () {return new odbccommand ();} // /// Establish DataAdapter object /// /// DataAdapter object public idbdataadapter createDataAdapter () {return new odbcdataadapter ();} /// / / / according to Connection establishing Transaction /// /// Connection Object /// Transaction Object public IDbTransaction CreateTransaction (IDbConnection myDbConnection) {return myDbConnection.BeginTransaction ();} /// /// Established DataReader /// /// DataReader object < / returns> public iDataReader CreateDataReader (idbcommand mydbcomma) Nd) {return mydbcommand.executeReader ();} /// /// get connection string /// /// public string getConnectionstring () {string strir = ConfigurationSettings.AppSettings [ "OdbcDriver"]; string strDBQ = ConfigurationSettings.AppSettings [ "OdbcDBQ"]; string strConnectionString = "Driver = {" strDriver "}; DBQ =" strDBQ ";"; return strConnectionString;

}}} Database Abstract Factory Implementation Factory (3) The following is the class that is really called when applying: (6) dbaccess.cs using system; use system.data; namespace dbservice {///

// / DBACCESS class, that is, the class /// public sealed class dbaccess {/// /// dbaccess constructor //// DBACCESS constructor /////////////////////////////////////////////////////////////////////////////////////////////////////>> /// /// unconditional query operation, ie all records in the query table //// /// table name /// column group group /// unconditional query results public static dataset selectall (string stratlename, string [] strcolumn) {dataset ds = new dataset (); factory factory = factory .GetInstance (); abstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString (); concreteDbConn.Open (); IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand (); IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction (concreteDbConn); concreteDbCommand.Connection = concreteDbConn; concreteDbCommand.Transaction = concreteDbTrans; IDbDataAdapter concreteDbAdapter = abstractDbFactory.CreateDataAdapter (); try {string strSql = "SELECT"; for (INT i = 0; i

(StrColumn [i] ",");} strSql = (strColumn [strColumn.Length - 1] "FROM" strTableName); concreteDbCommand.CommandText = strSql; concreteDbAdapter.SelectCommand = concreteDbCommand; concreteDbAdapter.Fill (ds) ConcretedbTrans.commit ();} catch {concretedbtrans.rollback (); ds.clear (); throw;} finally {contRededbconn.close ();} Return DS;} ///

/// condition query operation /// /// table name /// column name group /// Condition /// Condition Query Results public static dataset select (string stratlename, string [] strcolumn, string strcondition) {dataset ds = new dataset (); Factory factory = Factory.GetInstance (); AbstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString (); concreteDbConn.Open (); IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand ( ); Idbtransaction concretedbtrans = AbstractDbFactory.createTransaction (ConcretedbConn); concretedbcommand.connection = concretedbconn;

concreteDbCommand.Transaction = concreteDbTrans; IDbDataAdapter concreteDbAdapter = abstractDbFactory.CreateDataAdapter (); try {string strSql = "SELECT"; for (int i = 0; i /// single record Insert operation /// /// Table name /// column name group /// Value group public static void Insert (string strTableName, string [] strColumn, object [] strValue) {Factory factory = Factory.GetInstance (); abstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); Concretedbconn.connectionstring =

abstractDbFactory.GetConnectionString (); concreteDbConn.Open (); IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand (); IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction (concreteDbConn); concreteDbCommand.Connection = concreteDbConn; concreteDbCommand.Transaction = concreteDbTrans; try {string strSql = "INSERT INTO " STRTABLENAME " ("; for (int i = 0; I /// Batch record is inserted once Insert different batch records /// /// of the DataSet, the DataTable name in DataSet, the Datacolumn name, the DataTable, the Datacolumn name Name

public static void InsertSet (ref DataSet ds) {Factory factory = Factory.GetInstance (); AbstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString (); concreteDbConn.Open (); IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand (); IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction (concreteDbConn); concreteDbCommand.Connection = concreteDbConn; concreteDbCommand.Transaction = concreteDbTrans; try {foreach (DataTable dt in ds.Tables) {foreach (DataRow dr In dt.rows) {string strsql = "INSERT INTO" DT.TABLENAME "("; for (INT i = 0; i

Concretedbcommand.executenonquery ();}} contRedbtrans.commit ();} catch};} finally {contRetedbconn.close ();}} ///

/// unconditional delete operation, ie delete all records in the table /// /// table public static void DeleteAll (string strTableName) {Factory factory = Factory.GetInstance (); abstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString (); concreteDbConn.Open (); IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand (); IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransa ction (concreteDbConn); concreteDbCommand.Connection = concreteDbConn; concreteDbCommand.Transaction = concreteDbTrans; try {string strSql = "DELETE FROM" strTableName; concreteDbCommand.CommandText = strSql; concreteDbCommand.ExecuteNonQuery (); concreteDbTrans.Commit ();} catch { Concretedbtrans.rollback (); throw;} finally {contRedbconn.close ();}} ///

/// Condition Delete Operation /// /// Table Name /// condition public static void delete (string strTableName, string strCondition) {Factory factory = Factory.GetInstance (); abstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString (); concreteDbConn.Open () ; IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand (); IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction (concreteDbConn); concreteDbCommand.Connection = concreteDbConn; concreteDbCommand.Transaction = concreteDbTrans; try {string strSql = "DELETE FROM" strTableName "WHERE" strCondition; Concretedbcommand.commandtext = ST RSQL; contRedbcommand.executenonquery (); contRedbTrans.commit ();} catch {concretedbtrans.rollback (); throw;} finally {contretedbconn.close ();}} ///

/// unconditional update operation, ie Update all records /// /// table name /// column name group ///// Value group

public static void UpdateAll (string strTableName, string [] strColumn, object [] strValue) {Factory factory = Factory.GetInstance (); AbstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString (); concreteDbConn.Open (); IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand (); IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction (concreteDbConn); concreteDbCommand.Connection = concreteDbConn; concreteDbCommand.Transaction = concreteDbTrans; try {string strSql = "UPDATE " STRTABLENAME " set "; for (int i = 0; i /// condition update operation /// /// Table name /// column name group /// Value group /// conditions public static void Update (string strTableName, string [] strColumn, object [] strValue, string strCondition) {Factory factory = Factory.GetInstance (); abstractDbFactory abstractDbFactory = factory.CreateInstance (); IDbConnection concreteDbConn = abstractDbFactory.CreateConnection (); concreteDbConn.ConnectionString = abstractDbFactory.GetConnectionString (); concreteDbConn.Open (); IDbCommand concreteDbCommand = abstractDbFactory.CreateCommand (); IDbTransaction concreteDbTrans = abstractDbFactory.CreateTransaction (concreteDbConn); concreteDbCommand.Connection = concreteDbConn; Concretedbcommand.transaction = concretedbtrans; try {string strsql = "Update" strtablename "s ET "; for (int i = 0; I

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

New Post(0)