在我的网络应用中出现此错误。
该交易与当前连接无关或已完成。
#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
请参阅代码中的粗体文字。
希望听到您的积极回应。
此致
答案 0 :(得分:1)
发生错误是因为您使用了共享成员变量。
您可以使用共享功能,但您应该完全避免使用共享变量或成员 不要在共享上下文中“保存”任何信息。这些在应用程序范围内可用,对于来自不同用户的不同请求可能相同。
在共享(静态)函数中运行数据访问操作应该没问题。
但是,拥有共享成员会导致应用程序不稳定的完整性和并发性问题
将这些变量放在方法
中oSqlConnection As SqlConnection
oSqlDataAdapter As SqlDataAdapter
oSqlCommand As SqlCommand
oSqlTransaction As SqlTransaction
将解决问题。