在数据库连接上创建事务处理对象,然后调用事务处理对象来提交事务或回滚事务。简单的代码:
private void button1_Click(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=192.168.2.200;uid=sa; password=;database = HaierHR");
conn.Open();
//启用事务
SqlTransaction tran = conn.BeginTransaction();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.Transaction = tran;
try
{
cmd.CommandText = "UPDATE HRRollMain Set TotalMember=TotalMember-100 WHERE RollID = '2005070101'";
cmd.ExecuteNonQuery();
cmd.CommandText = "UPDATE HRRollSum Set TotalSumMember=TotalSumMember+100 WHERE RollSumID = '20050701'";
cmd.ExecuteNonQuery();
tran.Commit();
MessageBox.Show("事务提交成功!");
}
catch(Exception ex)
{
tran.Rollback();
MessageBox.Show("Error!"+ex.Message);
}
}
http://maxianghui.cnblogs.com/archive/2006/07/11/448173.html
Word教程网 | Excel教程网 | Dreamweaver教程网 | Fireworks教程网 | PPT教程网 | FLASH教程网 | PS教程网 |
HTML教程网 | DIV CSS教程网 | FLASH AS教程网 | ACCESS教程网 | SQL SERVER教程网 | C语言教程网 | JAVASCRIPT教程网 |
ASP教程网 | ASP.NET教程网 | CorelDraw教程网 |