/// /// file name: SQLDataUpdater.cs /// Abstract: SQL Data Upper, easy to read, write, delete the single data table, and the constructor is outgoing result set by reference. / // SqlDataUpdater DU = New SqlDataUpdater (SQLHERE, REF YourDataSet) /// Call Update (DS) update data, automatic transaction processing, error message existence errorMessage. // /// Completion date: 2005-4-21 /// Author: Sun Wei Ding /// current version: 1.0 /// summary> public class SqlDataUpdater {private SqlConnection cn; private SqlCommand cmd; private SqlDataAdapter da; private SqlTransaction tran; private SqlCommandBuilder cmdBuilder; private string err;
Public string error;}} /// /// Initialization data update /// summary> /// used to return SCHEMA SQL PARAM> /// Dataset by reference param> public sqldataupdater (String SQL, Ref Dataset Dataset) {Err = ""; if (dataset == null) DataSet = New DataSet (); Cn = new sqlConnection (zmarketdata.datasetting.connectionstring); cmd = new sqlcommand (SQL, CN); DA = New SqldataAdapter (cmd); cmdbuilder = new SQLCommandBuilder (DA);
Da.fill (Dataset);
Public Bool Update (DataSet DS) {BOOL SUCCESS = FALSE; cn.open (); TRAN = cn.begintransaction (); cmd.transaction = TRAN; try {gletchanges ()! = null) {Da.Update (DS.GetChanges ());} surcess = true; tran.commit ();} catch (exception ex) {tran.rollback (); err = ex. subssage;} = false;} Return Success;}
~ Sqldataupdater () {da.dispose (); cmd.dispose (); cn.dispose ();}}
// Application example
DataSet DS = New Dataset (); SqlDataUpdater DU = New SqldataUpdater ("Select * from public_feedback where 1 = 2", Ref DS); DATAROW DR = DS.TABLES [0] .newrow ();
DR ["" "] = request [" txtName "]; DR [" txtcompany "]; dr [" txtcompany "]; DR [" txtcompany "]; DR [" email "];" txtemail] = request ["txtemail "]; ds.tables [0] .Rows.Add (DR); if (du.Update (ds)) {// success} else {response.write (du.errorMessage);}