0

下面的代码中抛出了一个异常:

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

我今天花了五个小时看这个没有解决方案。任何人都可以提出一个查询线吗?

4

1 回答 1

0

我和你一样经历过(服务器“计算机名”上的 MSDTC 不可用自动出现)并尝试 http://geekswithblogs.net/narent/archive/2006/10/09/93544.aspx

----在触发器所在的服务器上,需要开启MSDTC服务。您可以通过单击开始 > 设置 > 控制面板 > 管理工具 > 服务来实现。找到名为“分布式事务协调器”的服务并右键单击(在其上并选择)> 开始。

它的工作正常。

ps:但我仍然不知道为什么服务器“计算机名”上的 MSDTC 不可用会自动出现

于 2013-12-18T10:35:41.243 回答