MySQL Transaction test

Source: Internet
Author: User

 Public decimaltransactiontest () {decimalv =0;using(Mysqlconnection con =Newmysqlconnection (connectionString)) {con. Open ();using(Mysqltransaction trans =con. BeginTransaction ()) {Try{using(Mysqlcommand cmd =NewMysqlcommand ("Select Debit_balance from C_account_customer where id=1 for update;", con, trans)) {v= (decimal) cmd. ExecuteScalar ();}using(Mysqlcommand cmd =NewMysqlcommand ("insert INTO Abacus.testvalue (val) values (@db);", con, trans)) {cmd. Parameters.addwithvalue ("@db", v); cmd. ExecuteNonQuery (); cmd. Parameters.clear ();}using(Mysqlcommand cmd =NewMysqlcommand ("INSERT INTO Abacus.c_entry_customer (customer_id, item_id, ref_id, direction, amount, operation, operation_id, note) VALUES (1, 1, 1, 1, 1, 1, 1, 1); ", con, trans)) {cmd. ExecuteNonQuery ();}using(Mysqlcommand cmd =NewMysqlcommand ("INSERT INTO Abacus.c_entry_customer (customer_id,item_id,ref_id,direction,amount,operation,operation_id,note) Values (1,2,1,1,1,1,1,1);", con, trans)) {cmd. ExecuteNonQuery ();}using(Mysqlcommand cmd =NewMysqlcommand ("update Abacus.c_account_customer Set [email protected]+1 where id=1;", con, trans)) {cmd. Parameters.addwithvalue ("@db", v); cmd. ExecuteNonQuery (); cmd. Parameters.clear ();} Trans.commit ();}Catch(Exception ex) {trans. Rollback ();}finally{con. Close ();}returnv; }}}

MySQL Transaction test

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.