单个 sqlconnection 中的多个 sqltransactions
我有一些想要执行的代码,如下所示。但我在第二次迭代时不断收到异常“此 SqlTransaction 已完成;它不再可用”。有人可以帮我指出我在这里做错了什么吗?谢谢!
SqlConnection cn = (SqlConnection)SqlConnectionManager.Instance.GetUserConnection(user);
cn.Open();
try
{
foreach (Master mRecord in masterList)
{
if (sqlTransaction == null)
sqlTransaction = cn.BeginTransaction();
SqlCommand cm = cn.CreateCommand();
cm.Transaction = sqlTransaction;
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "pr_InsertRecords";
try
{
cm.ExecuteNonQuery();
Debug.WriteLine("Auditor.Write: end sql table value param");
sqlTransaction.Commit();
sqlTransaction.Dispose();
}
catch (Exception Ex)
{
Debug.WriteLine(" Exception message: " + Ex.Message);
if (Ex.InnerException != null)
{
Debug.WriteLine("Inner exception message" + Ex.InnerException.Message);
}
sqlTransaction.Rollback();
}
}
}
finally
{
cn.Close();
}
I have some code that I want to execute as follows. But I keep getting the exception "This SqlTransaction has completed; it is no longer usable" on the 2nd iteration. Could someone help me point out what I am doing wrong here? Thanks!
SqlConnection cn = (SqlConnection)SqlConnectionManager.Instance.GetUserConnection(user);
cn.Open();
try
{
foreach (Master mRecord in masterList)
{
if (sqlTransaction == null)
sqlTransaction = cn.BeginTransaction();
SqlCommand cm = cn.CreateCommand();
cm.Transaction = sqlTransaction;
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = "pr_InsertRecords";
try
{
cm.ExecuteNonQuery();
Debug.WriteLine("Auditor.Write: end sql table value param");
sqlTransaction.Commit();
sqlTransaction.Dispose();
}
catch (Exception Ex)
{
Debug.WriteLine(" Exception message: " + Ex.Message);
if (Ex.InnerException != null)
{
Debug.WriteLine("Inner exception message" + Ex.InnerException.Message);
}
sqlTransaction.Rollback();
}
}
}
finally
{
cn.Close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在循环内,您可以提交或回滚,但不会将引用重置为
null
。SqlTransaction
通常不这样使用,而是在using()
块中使用,就像SqlConnection
一样:Inside the loop you either commit or rollback, but you do not reset the reference to
null
.SqlTransaction
in general is not used as this, is used in ausing()
block, just as aSqlConnection
is:尝试在处置后将
sqlTransaction
对象设置为 null。旁注,您确实应该将这些IDisposable
对象包装在 using 块中,以便始终调用Dispose
。Try setting your
sqlTransaction
object to null after disposing it. Side note, you really should be wrapping thoseIDisposable
objects in using blocks soDispose
is called at all times.您需要在每次迭代时创建一个新的
SqlTransaction
对象,或者如果您希望循环内的所有操作都在单个事务中发生,则需要将事务完全移出循环。提交事务后,需要在连接上再次调用 BeginTransaction 以启动新事务。您无法重用旧的事务对象。You need to create a new
SqlTransaction
object on each iteration or move the transaction completely outside the loop if you are wanting all the operations inside the loop to occur in a single transaction. Once you commit a transaction, it is necessary to call BeginTransaction again on the connection to start a new one. You cannot reuse the old transaction object.