该交易要么与当前连接无关,要么已完成 [英] The transaction is either not associated with the current connection or has been completed

查看:92
本文介绍了该交易要么与当前连接无关,要么已完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

If GlbSQLCOnn.State = ConnectionState.Closed Then
    GlbSQLCOnn.Open()
End If
'===========
SqlTrans = GlbSQLCOnn.BeginTransaction
Cmd.Transaction = SqlTrans
Cmd.CommandType = CommandType.Text
Cmd.CommandText = StrSQlDelete
Cmd.UpdatedRowSource = UpdateRowSource.None
Cmd.CommandTimeout = 10000

Cmd.ExecuteNonQuery()

Cmd.CommandText = StrSQlInsert & " OPTION (MAXDOP 2)"

Cmd.ExecuteNonQuery()


SqlTrans.Commit()

推荐答案

从您的代码看,只有我能想到的解释是你的SqlCommand Cmd 与你开始交易的SqlConnection GlbSQLCOnn 没有关联。



您的连接名称 GlbSQLCOnn 似乎表明它是一个全局连接对象。没有理由做那样的事情。默认情况下,与Sql-Server的连接是合并的 - 因此在应用程序的整个生命周期内重复使用相同的连接对象都无法获得任何好处。相反,它可能很快变得混乱(你的问题似乎就是这种情况)。这就是为什么我建议你只在本地使用任何SqlClient对象。



伪代码:

From what is to see of your code, the only explanation I can think of is that your SqlCommand Cmd isn't associated with the SqlConnection GlbSQLCOnn on which you started the transaction.

The name of your connection, GlbSQLCOnn, seems to indicate that it's a "global" connection object. There's no reason to do something like that. Connections to Sql-Server are pooled by default - so you gain nothing by re-using the same connection-object throughout the lifetime of your application. Instead it can become messy quickly (your problem here seems to be a case of that). That's why I suggest you to use any SqlClient-objects only locally.

Pseudo-code:
someMethod()
   using connection = new SqlConnection(connectionString)
   using transaction = connection.BeginTransaction
   using command = new SqlCommand(connection, transaction)

      connection.Open

      // do something here

      transaction.Commit

   end using
end method





即使我从第一段开始的假设不正确,你也可以坚持上述模式解决问题。



Even if my assumption from the first paragraph isn't correct, you will solve your problem by sticking to the above "pattern".


这篇关于该交易要么与当前连接无关,要么已完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆