如何在过程中回滚datatransaction [英] How to roll back the datatransaction in procedure
本文介绍了如何在过程中回滚datatransaction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用此代码,但是在插入数据时出错。该对象引用未设置。
我尝试过:
I have use this code but it is giving error when i insert the data. That object reference is not set.
What I have tried:
SqlTransaction tran=null;
try
{
con.Open();
//tran = con.BeginTransaction();
-----程序代码-----
----- code of procedure-----
cmd.ExecuteNonQuery();
//tran.Commit();
}
catch
{
tran.Rollback();
throw;
}
推荐答案
嗯......是的,它会:
Well ... yes, it will:
SqlTransaction tran=null;
你专门将它设置为null,所以它没有对象!
试试这个:
You specifically set it to null, so it doesn't have an object!
Try this:
SqlTransaction trans;
try
{
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
trans = con.BeginTransaction();
using (SqlCommand cmd = new SqlCommand("...", con))
{
...
}
trans.Commit();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
trans.Rollback();
}
这篇关于如何在过程中回滚datatransaction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文