事务操作一共有三种。
里面这样用事务,下面是我自己用事务时写的代码,希望对你有所帮助:
con.ConnectionString = ConfigurationManager.ConnectionStrings["DisplayDataConnectionString"].ConnectionString;
con.Open();
SqlTransaction trans = con.BeginTransaction();
cmd.Connection = con;
cmd.Transaction = trans;
try
{
= string.Format("delete from UserRole where UserID='{0}' ", id);
cmd.ExecuteNonQuery();
= string.Format("delete from UserInfo where UserID='{0}' ", id);
cmd.ExecuteNonQuery();
();//提交事务
}
catch (Exception e)
{
trans.Rollback();//回滚
throw e;
}
finally
{
cmd.Dispose();
con.Close();
}