When using ADO.NET, each database operation is set to set the connection property, establish a connection, use Command, transaction processing, etc., more cumbersome, there are many duplicates. Can you add these cumbersome and commonly used operations to use it more convenient and safely.
The following is an attempt class: using System; using System.Data.SqlClient; using System.Text; using System.Data; using System.Collections; using System.Configuration; public class DBAccess {///
///
dataAdapterCommand.SelectCommand.CommandText = value;}} ///
Exp.Message "Check the SQL"; Return Breturn = false;}}} else =} {Return Breturn;} else {INT NAFFECTED = -1; if (DBDataReader! = null) {dbdataareader .Close (); dbDataReader = null;} /// get the transaction object from the connection SqlTransaction trans = dbConnection.BeginTransaction (); try {/// create a nested transaction on the connection transaction switch (command) {case COMMAND. DELETE: dbDeleteCommand.Transaction = trans; break; case COMMAND.INSERT: dbInsertCommand.Transaction = trans; break; case COMMAND.UPDATE: dbUpdateCommand.Transaction = trans; break;} /// execute the command switch (command) {case COMMAND . DelteCommand.executenonQuery (); break; case command.insert: naffected = dbinsertcommand.executenonQuery (); break; cascand.Update: naffected = dbupd ateCommand.ExecuteNonQuery (); break;}} catch (InvalidOperationException ioexp) {StringBuilder buildError = new StringBuilder (); buildError.Append ( "InvalidOperationException thrown when trying to"); switch (command) {case COMMAND.DELETE: buildError.Append ("Delete"); Break; Case Command.insert: Builderror.Append ("insert"); Break; Case Command.Uden: Builderror.Append ("Update"); Break;} builderror.Append (", error given = " ioExp.Message " Check the SQL "); error = builderror.tostring (); return breturn =
false;} catch (SqlException dbexp) {StringBuilder buildError = new StringBuilder (); buildError.Append ( "InvalidOperationException thrown when trying to"); switch (command) {case COMMAND.DELETE: buildError.Append ( "Delete"); break Case Command.insert: Builderror.Append ("insert"); Break; Case Command.Update: Builderror.Append ("Update"); Break;} builderror.Append (", error Given =" dbExp.Message " Check the SQL "); error = builderror.tostring (); return breturn = false;} finally {/// commit the command if (NAFFECTED == 1) {switch {copy command.delete: dbdeleteCommand.Transaction. Commit (); break; break; case; break; break; break; breturn;} Else /// if Something Went Wrong rollback {switch (command) {case COMMAND.DELETE: dbDeleteCommand.Transaction.Rollback (); break; case COMMAND.INSERT: dbInsertCommand.Transaction.Rollback (); break; case COMMAND.UPDATE: dbUpdateCommand.Transaction.Rollback (); Break;} //trans.rollback (); breturn = false;}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} #region select functions ///
false) {dbDataReader.Close (); dbDataReader = null;}} dbDataReader = dbSelectCommand.ExecuteReader (); return dbDataReader;}} ///
new SqlCommand (); /// set up the adapter dbDataAdapter.DeleteCommand = dbDeleteCommand; dbDataAdapter.InsertCommand = dbInsertCommand; dbDataAdapter.SelectCommand = dbSelectCommand; dbDataAdapter.UpdateCommand = dbUpdateCommand; /// make sure everyone knows what conection to use dbSelectCommand.Connection = dbConnection; dbDeleteCommand.Connection = dbConnection; dbUpdateCommand.Connection = dbConnection; dbInsertCommand.Connection = dbConnection; command = COMMAND.NONE; dbDataReader = null; dbSelectCommandofAdapter = new SqlCommand (); dataAdapterCommand = new SqlDataAdapter (); dataAdapterCommand.SelectCommand = dbSelectCommandofAdapter ;} public void Open () {/// set up the connection string StringBuilder strBuild = new StringBuilder (); // Connection properties reading strBuild.AppendFormat (ConfigurationSettings.AppSettings [ "dBConnection"]) from the configuration file; dbConnection. Connectionstring = strbuild.toString (); try {dbconnect ION.Open (); bopen = true;} catch (exception exp) {error = exp .MESSAGE;}} ///
INSERT INTO UserTable (USRNAME, PWD, Name, Depart, role, available) values ("); sqlstr.append (" " usrname " "); sqlstr.Append (" " pwd ", "); Sqlstr.append (" '" name " "); sqlstr.Append ("' " divart "); sqlstr.append ("" role "", ") ; sqlStr.Append (1); sqlStr.Append ( ")"); newUserDBAccess.InsertCommand = sqlStr.ToString (); newUserDBAccess.Open ();! try {if (newUserDBAccess.ExecuteCommand ()) {errMsg = newUserDBAccess.ErrorMessage ; return false;} else {return true;}} finally {newUserDBAccess.Close ();}} Update operation, modify user information: public bool ModifyUser () {DBAccess modifyUserDBAccess = new DBAccess (); StringBuilder sqlStr = new StringBuilder () SQLSTR.APpend ("Update UserTable Set"; sqlstr.append ("USRNAME ="); SQLSTR.APpend ("'" usrname "); sqlstr.append (" name = "); SQLSTR. Append ("'" name "" "); sqlstr.append (" pwd = "); sqlstr.append (" " pwd ", "); Sqlstr.append (" depart = "); sqlstr.append (" '" depart " "; sqlstr.Append (" role = "); sqlstr.append (" " role " '"); Sqlstr.append (" where usrid = "); sqlstr.Append (ID); modifyUserdbaccess.UpdateCommand = SQLSTR.TOSTRING (); ModifyUserdbaccess.open ();