码迷,mamicode.com
首页 > 数据库 > 详细

mysql 事务测试

时间:2016-01-12 11:32:27      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

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;
}
}
}

 

mysql 事务测试

标签:

原文地址:http://www.cnblogs.com/zhangzhi19861216/p/5123527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!