vb.net 错误 - 事务与当前连接无关或已完成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15481488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error - The transaction is either not associated with the current connection or has been completed
提问by Gimo Gilmore
Having this error in my web app.
在我的网络应用程序中出现此错误。
The transaction is either not associated with the current connection or has been completed.
事务要么与当前连接无关,要么已完成。
#Region "Database Queries"
Private Shared oSqlConnection As SqlConnection
Private Shared oSqlDataAdapter As SqlDataAdapter
Private Shared oSqlCommand As SqlCommand
Private Shared oSqlTransaction As SqlTransaction
Private Shared _strCommand As String
Public Shared Property strCommand() As String
Get
Return _strCommand
End Get
Set(ByVal value As String)
If Not InTransaction Then RollBack_Transaction()
_strCommand = "SET DATEFORMAT mdy " & vbCrLf & value
DbQuery()
End Set
End Property
Protected Shared Sub DbQuery()
'Try
If Not InTransaction Then
RollBack_Transaction()
oSqlCommand = New SqlCommand(_strCommand, oSqlConnection)
Else
oSqlCommand = oSqlConnection.CreateCommand
If _InTransaction_Initial Then
oSqlConnection.Open()
oSqlTransaction = oSqlConnection.BeginTransaction(IsolationLevel.ReadCommitted)
_InTransaction_Initial = False
End If
oSqlCommand.Transaction = oSqlTransaction
oSqlCommand.CommandText = _strCommand
oSqlCommand.CommandTimeout = 0
End If
oSqlDataAdapter = New SqlDataAdapter(oSqlCommand)
Ds = New DataSet
oSqlDataAdapter.Fill(Ds) '**<- - - The error occurs here**'
'Catch ex As Exception
' SetMessage(ex)
' ' MsgBox("Server: " & ServerName & vbCrLf & "User Id: " & SqlID & vbCrLf & "Password: " & SqlPassword & vbCrLf & "Database: " & MainDb)
'End Try
'oSqlConnection.Close()
End Sub
Private Shared _InTransaction_Initial As Boolean = False
Private Shared InTransaction As Boolean
Public Shared Sub StartTransaction()
_InTransaction_Initial = True
InTransaction = True
End Sub
Public Shared Sub RollBack_Transaction()
Try
oSqlTransaction.Rollback()
Catch ex As Exception
End Try
Try
oSqlConnection.Close()
Catch ex As Exception
End Try
InTransaction = False
End Sub
Public Shared Sub Commit_Transaction()
Try
oSqlTransaction.Commit()
Catch ex As Exception
End Try
Try
oSqlConnection.Close()
Catch ex As Exception
End Try
InTransaction = False
End Sub
Function dsCount() As Long
Return Ds.Tables(0).Rows.Count
End Function
Please see the Bold text in the code.
请参阅代码中的粗体文本。
Hope to hear positive response from you.
希望听到你的积极回应。
Regards,
问候,
回答by Mohsen Heydari
It error happens because you have used Shared member variables.
发生错误是因为您使用了共享成员变量。
You can uses shared functions, but you should avoid shared variables or members at all.
Don't "save" any information in shared context. These are available in application scope and that may be the same for different requests from different users.
您可以使用共享函数,但您应该完全避免共享变量或成员。
不要在共享上下文中“保存”任何信息。这些在应用程序范围内可用,并且对于来自不同用户的不同请求可能是相同的。
Running data access operations in shared (static) functions should be no problem.
But having shared members will cause integrity and concurrency problems making the application unstable
在共享(静态)函数中运行数据访问操作应该没有问题。
但是共享成员会导致完整性和并发问题,使应用程序不稳定
placing these as variables inside methods
将这些作为变量放在方法中
oSqlConnection As SqlConnection
oSqlDataAdapter As SqlDataAdapter
oSqlCommand As SqlCommand
oSqlTransaction As SqlTransaction
will solve the problem.
将解决问题。

