下面的代码中抛出了一个异常:
Imports System.Transactions
Imports System.Data.SqlClient
Public Class _Default
Inherits System.Web.UI.Page
Public Sub Test()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Using scope As New TransactionScope()
Test(1)
Test(2)
Test(3)
Test2(4)
End Using
Catch ex As Exception
End Try
End Sub
Public Sub Test(ByVal personID As Integer)
Try
Dim objCommand As New SqlCommand
Dim strConString As String = "Data Source=IANSCOMPUTER;Initial Catalog=AcornTest;Integrated Security=True"
Dim objCon As New SqlConnection
objCon.ConnectionString = strConString
objCon.Open()
Using objCon
Using objCommand
objCommand.Connection = objCon
objCommand.CommandText = "UPDATE Person SET URN = @URN "
objCommand.Parameters.AddWithValue("@URN", personID)
objCommand.ExecuteNonQuery()
End Using
End Using
objCon.Close()
objCon = Nothing
Catch ex As Exception
'I don't swallow exceptions.
End Try
End Sub
Public Sub Test2(ByVal personID As Integer)
Try
Dim objCommand As New SqlCommand
Dim strConString As String = "Data Source=IANSCOMPUTER;Initial Catalog=AdventureWorks2008R2;Integrated Security=True"
Dim objCon As New SqlConnection
objCon.ConnectionString = strConString
objCon.Open()
Using objCon
Using objCommand
objCommand.Connection = objCon
objCommand.CommandText = "UPDATE Person SET URN = @URN "
objCommand.Parameters.AddWithValue("@URN", personID)
objCommand.ExecuteNonQuery()
End Using
End Using
objCon.Close()
objCon = Nothing
Catch ex As Exception
'I don't swallow exceptions.
End Try
End Sub
End Class
我认为 TransactionScope 允许您创建分布式事务。例外情况是:“服务器 'IANSCOMPUTER' 上的 MSDTC 不可用。” 在执行 Test2.objCon.Open 时。
更新 Cserg 的评论有帮助。我正在尝试识别使用 SQLTransaction 实例的企业应用程序中的错误 - 我正在转移到 TransactionScope。我在测试系统的日志文件中看到了这个错误:
The Transaction has already been implicitly or explicitly committed or aborted
我今天花了五个小时看这个没有解决方案。任何人都可以提出一个查询线吗?