赵国梁 发表于 2012-2-29 12:28
你这个是SQL中实物的写法,我问的是ADO.NET中事务的写法!
不好意思,有点马虎了哈!- SqlConnection conn = new SqlConnection("Data Source=.;uid=sa;passWord=;database = BankSystem");
- conn.Open();
- //启用事务
- SqlTransaction tran = conn.BeginTransaction();
- SqlCommand cmd = new SqlCommand();
- cmd.Connection = conn;
- cmd.Transaction = tran;
- try
- {
- cmd.CommandText = "UPDATE bank SET saveMoney=saveMoney-1000 WHERE customerName='张三'";
- cmd.ExecuteNonQuery();
- cmd.CommandText = "UPDATE bank SET saveMoney=saveMoney+1000 WHERE customerName='李四'";
- cmd.ExecuteNonQuery();
- tran.Commit();
- MessageBox.Show("事务提交成功!");
- }
- catch(Exception ex)
- {
- tran.Rollback();
- MessageBox.Show("事务提交失败!"+ex.Message);
- }
复制代码 |