SQL Server transaction Processing

Source: Internet
Author: User

1         /// <summary>2         ///SQL Server transaction processing3         /// </summary>4         /// <param name= "Sender" ></param>5         /// <param name= "E" ></param>6         Private voidBtn_tran_click (Objectsender, EventArgs e)7         {8             //------------------------------------------------------------//9 TenSqlConnection con =NewSqlConnection (server = (local); database = Freenotes; UID = HF; PWD =123); One  A             //------------------------------------------------------------// -              -             //* * * Bank account to transfer money the  -             //account balance used to obtain the login card number -             intU1_money =Getmoney (Login.AccountNum.Tostring ()); -  +             //used to get the amount of money to be transferred -             intMoney = Int32.Parse ( This. Txtmonrey.text); +  A             //this is achieved by subtracting the balance of the AccountNum account after the transfer amount.  at             intUl_balance = U1_money-Money ; -  -             //if the amount to be transferred is greater than the amount of the card, the balance is insufficient.  -             if(Ul_balance <0) -             { -MessageBox.Show ("Insufficient account balance! ","Tips"); in             } -             //------------------------------------------------------------// to  +             //* * * Bank account to receive money -  the             //Get the balance information to transfer to your Txtinaccountmoney account *             intU2_money =Getmoney (TxtInAccountmoney.Text.Trim ()); $ Panax Notoginseng             //at this point, you get the balance of the other account plus the total of the amount that will be transferred.  -             intU2_balance = U2_money +Money ; the  +  A             //------------------------------------------------------------// the  +             //SQL statements -  $             //SQL statement to transfer money $             stringSqlin =""; -  -             //SQL statement for money transfer the             stringSqlout =""; - Wuyi             //Open a database connection the con. Open (); -  Wu             //Create transaction Object -SqlTransaction st =con. BeginTransaction (); About  $             //Create an SqlCommand object for account 1 -SqlCommand cmd1 =NewSqlCommand (Sqlin, con); -  -             //Create an SqlCommand object for account 2 ASqlCommand CMD2 =NewSqlCommand (sqlout, con); +  the             //Add object 1 to the transaction object St -Cmd1. Transaction =St; $  the             //Add object 2 to the transaction object St theCmd2. Transaction =St; the  the             Try -             { in                 //returns the number of rows executed, using two SQL statements, so it is two.  the                 inti = cmd1. ExecuteNonQuery () +CMD2. ExecuteNonQuery (); the  About                 //the judgment is equal to 2, which equals two succeeds in execution.  the                 if(I! =2) the                 { the                     //not equal to 2, throws an exception!  +                     Throw NewException (); -                 } the                 ElseBayi                 { theMessageBox.Show ("Transfer Success! ","Tips"); the                 } -             } -             Catch(Exception ex) the             { the St. Rollback (); theMessageBox.Show ("Transfer failed! ","Tips"); the             } -             finally the             { the con. Close (); the             }94}

SQL Server transaction Processing

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: 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.