Database abstract factory implemented with C # (3)

zhaozj2021-02-16  54

Database abstract factory implemented with C # (3)

The following is the class that is really called when the application is applied:

(6) DBACCESS.CS

Using system; using system.data;

Namespace DBService {///

/// DBACCESS class, that is, the class /// Public Sealed Class Dbaccess {/// /// DBACCESS constructor // / private dbaccess () {}

///

/// 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

///

// / 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 < StrColumn.Length - 1; i ) {strsql = (strcolumn [i] ",");} strsql = (strcolumn [strcolumn.length - 1] "from" strtablename "Where" strcondition); concreteDbCommand.CommandText = strSql; concreteDbAdapter.SelectCommand = concreteDbCommand; concreteDbAdapter.Fill (ds); concreteDbTrans.Commit ();} catch {concreteDbTrans.Rollback (); ds.Clear (); throw;} finally {concreteDbConn.Close () } Return ds;}

///

/// Single record insert /// /// table name /// column Group /// Value group public static void insert (string stratablename, 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 records, you can insert different batch records /////////////////////////////////////////////////////////////////////////////////////> DataSet, each DataTable named table names DataSet, DataTable DataColumn in each column name called 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 = contRedbtrans; try {foreach (DataTable DT IN DS.TABLES) {finach (DATAROW DR IN DT.ROWS) {string strsql = "INTO" DT.TABLENAME "("; for (int i = 0; i

///

/// unconditional delete operation, that is, delete all records in the table /// /// table name 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.CreateTransaction (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 {contRetedbconn.close ();}}

///

/// /// 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; contRedbcommand.commandtext = strsql; contRedbcommand.executenonQuery (); contRedbtrans.commit ();} catch {contRedbtrans.rollback (); throw;} Finally {concretedbconn.close ();}}

///

/// unconditional update operation, that is, update all records in the table /// /// table name /// column name group /// value group public static void updateall (string stratlename, 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 /// condition public static void update (string stratlename, 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 "SET"; for ( INT i = 0; i

In the last step, add some variables for the database connection string under the root node Configuration in Web.config:

Now everything is OK, you can use different database connections (SQLServer private connections, OLEDB connections, and ODBC connections) through variables in web.config, while the entire use is still through DBACCESS, which is not affected. Welcome everyone to criticize :)

Full text

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

New Post(0)