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 /// summary> Public Sealed Class Dbaccess {/// /// DBACCESS constructor // / summary> private dbaccess () {}
/// /// unconditional query operation, ie all records in the query table //// summary> /// table name param> /// column group group param> /// unconditional query results returns> 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 /// summary> /// table name param> /// column name group param> /// condition param> /// condition query results returns> 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 /// summary> /// table name param> /// column Group param> /// Value group param> 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 param> 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 /// summary> /// table name param> 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 ();}}
/// /// summary> /// table name param> /// condition param> 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 /// summary> /// table name param> /// column name group param> /// value group param> 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 /// summary> /// Table name param> /// column name group param> /// Value group param> /// condition param> 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: appsettings>
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