我有这个代码用于将数据插入数据库。在我提交之前,我想确保数据输入成功,然后提交其他回滚两个插入。
try {
statement one commit;
statement two commit;
}
catch {
error message
statement 1 Rollback();
statement 2 Rollback();
}
finally {
if (con.State == Con.Open)
con.Close();
}
答案 0 :(得分:0)
你非常接近这样做,你只需要在两个语句之后放一个commit
语句,如果代码到达那一点,这意味着没有任何一个抛出异常,所以你可以执行提交,因为您确定这两个语句已成功执行。
try
{
statement one;
statement two;
commit;
}
catch
{
error message
statement 1 Rollback();
statement 2 Rollback();
}
finally
{
if (con.State == Con.Open)
con.Close();
}