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