1. Set up list for storing multiple statements
<summary>///save 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, 140025)); 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, 140025)); bool Bol = executetransaction (list); if (BOL) { MessageBox.Show ("saved successfully! "); } else { MessageBox.Show ("Save failed! "); }}
2. Call the Executetransaction method and return a return value of true for success, false for failure, statement and rollback
///<summary>///EXECUTE 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; } }}