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. Below this is an attempt:
Using system; using system.text; using system.data; using system.collections;
Public class dbaccess {///
///
Private sqlcommand dbselectcommandofadapter;
///
///
///
///
///
///
///
///
///
///
public string SelectDataSetCommand {get {return dataAdapterCommand.SelectCommand.CommandText;} set {command = COMMAND.DATASET; dataAdapterCommand.SelectCommand.CommandText = value;}} ///
Public DataSet getDataSet {get {switch (command) {copy command.dataset: return selectDataSet (); default: return null;}}}
public DataSet SelectDataSet () {try {dataAdapterCommand.SelectCommand.Connection = dbConnection; DataSet dataset = new DataSet (); dataAdapterCommand.Fill (dataset); return dataset;} catch (Exception exp) {error = exp.Message; return null; }
}
///
Try {if (DBDataReader! = null) {dbdataareader.close (); dbdataareader = null;}
bReturn = true; /// return bReturn;} catch (SqlException exp) {error = "dbException thrown when trying to Select, error given =" exp.Message "check the sql"; return bReturn = false;}} else IF (Command == Command.dataset) {Return Breturn;} else {INT NAFFECTED = -1;
IF (DBDataReader! = null) {dbdataareader.close (); dbdataareader = 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.DELETE: nAffected = dbDeleteCommand.ExecuteNonQuery (); break; case COMMAND.INSERT: nAffected = dbInsertCommand.ExecuteNonQuery (); break; case COMMAND.UPDATE: nAffected = dbUpdateCommand. ExecutenonQuery (); Break;}
} Catch (invalidoperationException oExp) {stringbuilder builderror = new stringbuilder (); builderror.Append ("InvalidOperationException THROWN WHEN TRYING TO);
Switch (command) {copy 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");
Switch (command) {copy command.delete: builderror.Append ("delete"); break; case command.insert: builderror.Append ("insert"); Break; Case Command.Uden: 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 (command) {case COMMAND.DELETE: dbDeleteCommand.Transaction.Commit (); break; case COMMAND.INSERT: dbInsertCommand.Transaction .Commit (); break; copy command.update: dbupdatecommand.transaction.commit (); break;}
//trans.commit ();
bReturn = true;} 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;}}}
Return Breturn;
#Region Select Functions
///
///
DBDataReader = dbselectcommand.executeReader (); return dbdatarader;}}
///
DBDataReader = dbselectcommand.executeReader (); return dbdatarader;}}
///
DBDataReader = dbselectcommand.executeReader (); return dbdatarader;}}
#ndregion
///
// create the command variables dbDataAdapter = new SqlDataAdapter (); dbConnection = new SqlConnection (); dbSelectCommand = new SqlCommand (); dbDeleteCommand = new SqlCommand (); dbUpdateCommand = new SqlCommand (); dbInsertCommand = 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; dbdataareader = null;
DBSelectCommandofadapter = new sqlcommand (); dataadaptercommand = new sqldataadapter (); dataadaptercommand.selectCommand = dbselectcommandofadapter;
Public void open () {/// set up the connection string stringbuilder strbuild = new stringbuilder ();
// Connection's property reads strbuild.appendformat ("dbconnection"); dbconnection.connectionstring = strbuild.connectionstring = strbuild.tostring ();
Try {dbconnection.open (); bopen = true;} catch (exception exp) {error = exp.Message;}
}
///
dbconnection.close ();
}
Use example:
Insert operation, the new user: public bool NewUser () {DBAccess newUserDBAccess = new DBAccess (); StringBuilder sqlStr = new StringBuilder (); sqlStr.Append ( "Insert into userTable (usrName, pwd, name, depart, role, available) values ("); Sqlstr.append (" " usrname " "; sqlstr.Append (" " pwd " "); sqlstr.Append (" " Name ", " SQLSTR.APpend ("'" depart ""; 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 ("'" divart "); sqlstr.Append (" role = "); sqlstr.Append ("'" Role "'); sqlstr.Append (" where usrid = "); sqlstr.Append (ID); modify = sqlstr.UpdateCommand = SQLSTR.TOSTRING (); ModifyUserDbaccess.open (); try {= !..................................................... ..
Delete operation, delete users: public static bool DelUser (int usrID) {DBAccess delUserDBAccess = new DBAccess (); StringBuilder sqlStr = new StringBuilder (); sqlStr.Append ( "update userTable set"); sqlStr.Append ( "available =" ); Sqlstr.Append (0); sqlstr.Append ("where usrid ="); sqlstr.append (usrid); deluserdbaccess.UpdateCommand = SQLSTR.TOSTRING (); deluserdbaccess.open (); try {if (! DELUSERDBACCESS. ExecuteCommand ()) {Return False;}}} finally {deluserdbaccess.close ();}}