我有一些代码正在做这样的事情(请注意,实际代码要复杂得多)。我制作了这个来帮助解释:
Imports System.Data.SqlClient
Imports System.Transactions
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Using scope As New TransactionScope
Try
Dim objCon As SqlConnection = New SqlConnection("Connection string 1")
Dim objCon2 As SqlConnection = New SqlConnection("Connection string 2")
objCon2.Open()
Using objCon2
Dim t1 As New Test
t1.Test()
objCon.Open()
End Using
Catch ex As Exception
'I do not swallow exceptions
End Try
End Using
End Sub
End Class
Public Class Test
Public Sub Test()
Dim objCon1 As SqlConnection = New SqlConnection("Connection string 1")
objCon1.Open()
End Sub
End Class
上面的代码显示我正在连接事务中的两个数据库(其中一个连接了两次)。这是一个例外:FormLoad 错误:System.Transactions.TransactionException:合作伙伴事务管理器已禁用其对远程/网络事务的支持。(来自 HRESULT 的异常:0x8004D025)。