mysql 事务测试
public decimal TransactionTest() { decimal v = 0; using (MySqlConnection con = new MySqlConnection(connectionString)) { con.Open(); using (MySqlTransaction trans = con.BeginTransaction()) { try { using (MySqlCommand cmd = new MySqlCommand("select debit_balance from c_account_customer where id=1 for update;", con, trans)) { v = (decimal)cmd.ExecuteScalar(); } using (MySqlCommand cmd = new MySqlCommand("insert into abacus.testvalue (val) values (@db);", con, trans)) { cmd.Parameters.AddWithValue("@db", v); cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); } using (MySqlCommand cmd = new MySqlCommand(" 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 = new MySqlCommand(" 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 = new MySqlCommand("update abacus.c_account_customer set debit_balance=@db+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(); } return v; } } }