A transaction is used in the program to manage the execution of the SQL statement, and the rollback requirement can be reached when execution fails.

Source: Internet
Author: User

1. Setting SQL execution statements using transactions

1         /// <summary>2         ///using SQL statements that have transactions3         /// </summary>4         /// <param name= "SQL" ></param>5         /// <param name= "conn" ></param>6         /// <param name= "Tran" ></param>7         /// <returns></returns>8          Public intExecsql (stringsql,sqlconnection conn,sqltransaction Tran)9         {Ten Openconn (conn); OneSqlCommand cmd =NewSqlCommand (SQL, conn); ACmd. Transaction =Tran; -             inti =cmd. ExecuteNonQuery (); -Console.WriteLine (string. Format ("Execute with Transaction: Impact: {0} line; statement: {1}", I, SQL)); the             returni; -}

2. Calling in the program

1 using(SqlTransaction tran =Conn. BeginTransaction ())2             {3                 Try4                 {5                     if(Dgvmast.currentrow! =NULL)6                     {7   Program.DB.Execsql (SQLSUB1, Conn, Tran);
Program. Db. Execsql (SQLSUB2, Conn, Tran); - Tran.commit (); //Transaction submission the } - } - Catch(Exception ex)//For exception handling - { + Tran. Rollback (); //Abnormal time, transaction rollback -MessageBox.Show (ex. Message,MessageBoxButtons.OK, messageboxicon.error); + } A at}

The consistency that can be performed to require SQL statements.

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: info-contact@alibabacloud.com 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.