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