creates a transaction. creates a transaction.

Source: Internet
Author: User creates a transaction. 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 ;}}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.