Asp.net creates a transaction. asp.net creates a transaction.
1. Create a List to store multiple statements.
/// <Summary> /// save the form /// </summary> /// <param name = "context"> </param> protected void save () {List <string> list = new List <string> (); list. add (string. format ("insert into picsone (model, idser, idflg, lmuser, lmdate, lmtime) values ('{0}', '{1}', '{2 }', '{3}', {4}, {5}) "," T1002 "," Y "," N "," U001 ", 20161103,140 025); list. add (string. format ("insert into picstwo (model, idser, idflg, lmuser, lmdate, lmtime) values ('{0 }' , '{1}', '{2}', '{3}', {4}, {5}) "," T1002 "," Y ", "N", "U001", 20161103,140 025); bool bol = ExecuteTransaction (list); if (bol) {MessageBox. show ("saved successfully! ");} Else {MessageBox. Show (" failed to save! ");}}
2. Call the ExecuteTransaction method, and return true to success, false to failure, and rollback.
/// <Summary> /// execute the statement /// </summary> /// <param name = "list"> </param> /// <returns> </returns> private bool ExecuteTransaction (List <string> list) {using (SqlConnection connection = new SqlConnection (ConfigurationManager. appSettings ["LocalConnectionString"]. toString () {SqlCommand command = new SqlCommand (); SqlTransaction transaction = null; try {connection. open (); transaction = connection. beginTransaction (); command. connection = connection; command. transaction = transaction; for (int I = 0; I <list. count; I ++) {command. commandText = list [I]; command. executeNonQuery ();} transaction. commit (); connection. close (); return true;} catch {transaction. rollback (); connection. close (); return false ;}}}