0

我们正在使用 SMO 在 SQL Server 2016 上将表从一台服务器传输到另一台服务器。

有一段时间,仅在 1 台服务器上传输仅 3 次成功,而且我们总是收到相同的错误消息,并没有说明太多:传输数据时发生错误。有关详细信息,请参阅内部异常。

相同的包已部署到我们的 4 个环境(Dev、Test、Qa 和 Prod)并且每天都在运行。但是,我们只在测试中遇到了这个问题。

我们的 DBA 并没有做太多的工作来帮助我们找到附加的原因。你能告诉我在哪里看吗?那个SSIS包很久没有更新了,构建的版本在所有环境下都是一样的。

这是我们在包中的 .vb 脚本:

Public Sub Main()
    '
    ' Source and Destination Servers
    '
    ' MsgBox(Dts.Variables("User::SourceServer").Value.ToString)
    Dim srvSourceName As String = Dts.Variables("SourceServer").Value.ToString  '"brdsqldev"
    Dim srvDestinationName As String = Dts.Variables("DestinationServer").Value.ToString  '"brdsqldev"
    Dim bt(0) As Byte
    '
    ' Source and Destination Databases
    '
    Dim dbSourceName As String = Dts.Variables("SourceDB").Value.ToString
    Dim dbDestinationName As String = Dts.Variables("DestinationDB").Value.ToString

    Try
        Dts.Log("The Transfer starts", 0, bt)
        Dts.Log("Transfer starts with tables", 0, bt)
        Dts.Log("Transfer starts with tables" & srvSourceName, 0, bt)
        Dts.Log("Transfer starts with tables" & srvDestinationName, 0, bt)
        Dts.Log("Transfer starts with tables" & dbSourceName, 0, bt)
        Dts.Log("Transfer starts with tables" & dbDestinationName, 0, bt)
        ' TableExists()
        Transfer(srvSourceName, srvDestinationName, dbSourceName, dbDestinationName, CType(Dts.Variables.Item("myDataTable").Value, System.Data.DataTable), TransferObjectType.Table)
        ' Transfer(srvSourceName, srvDestinationName, dbSourceName, dbDestinationName, CType(Dts.Variables.Item("myDataViews").Value, System.Data.DataTable), TransferObjectType.View)
        Dts.Log("Transfer done with tables", 0, bt)
        Dts.Log("The Transfer ran successfully", 0, bt)
        Dts.TaskResult = ScriptResults.Success
        'MessageBox.Show("The transfer ran successfully.")

    Catch ex As Exception
        Dts.Events.FireError(99, "", ex.Message.ToString(), "", -1)
        OutPutError(ex)
        Dts.TaskResult = ScriptResults.Failure
        'MessageBox.Show("The transfer was aborted.")
    End Try

End Sub


Private Sub Transfer(ByVal srvSourceName As String, ByVal srvDestinationName As String, ByVal dbSourceName As String, ByVal dbDestinationName As String, ByVal dataTableToTransfer As DataTable, ByVal type As TransferObjectType)
    Try
        Dim srcsrv As Server
        srcsrv = New Server(srvSourceName)

        Dim dbSource As Database
        dbSource = srcsrv.Databases(dbSourceName)

        Dim dessrv As Server
        dessrv = New Server(srvDestinationName)

        Dim dbDestination As Database
        dbDestination = dessrv.Databases(dbDestinationName)

        Dim oleDA As New OleDbDataAdapter
        Dim dt As New DataTable
        Dim row As DataRow
        Dim sMsg As String

        Dim xfr As Transfer

        Dim bt(0) As Byte

        Dts.Log("Transfer Subroutine starts ...", 0, bt)
        xfr = New Transfer(dbSource)

        xfr.CopyAllTables = False
        xfr.Options.WithDependencies = False
        xfr.Options.ContinueScriptingOnError = False

        xfr.CopyAllRoles = False
        xfr.CopyAllLogins = False
        xfr.CopyAllDatabaseTriggers = False
        xfr.CopyAllDefaults = False
        xfr.CopyAllPartitionFunctions = False
        xfr.CopyAllObjects = False
        xfr.CopyAllPartitionFunctions = False
        xfr.CopyAllPartitionSchemes = False
        xfr.CopyAllRules = False
        xfr.CopyAllSchemas = False
        xfr.CopyAllSqlAssemblies = False
        xfr.CopyAllStoredProcedures = False
        xfr.CopyAllSynonyms = False
        xfr.CopyAllUserDefinedAggregates = False
        xfr.CopyAllUserDefinedDataTypes = False
        xfr.CopyAllUserDefinedFunctions = False
        xfr.CopyAllUserDefinedTypes = False
        xfr.CopyAllUsers = False
        xfr.CopyAllViews = False
        xfr.CopyAllXmlSchemaCollections = False
        xfr.CopySchema = True

        xfr.DestinationDatabase = dbDestination.Name
        xfr.DestinationServer = dessrv.Name
        xfr.DestinationLoginSecure = True
        xfr.DropDestinationObjectsFirst = False

        Select Case type
            Case TransferObjectType.Table
                xfr.CopyData = True
            Case TransferObjectType.View
                xfr.CopyData = False
            Case Else
                Dts.Log("Unknown object transfer type. (-99)", -99, bt)
                Dts.TaskResult = ScriptResults.Failure
                'Throw New Exception("Unknown object transfer type.")
        End Select

        dt = dataTableToTransfer
        Dts.Log("Transfer Subroutine just before Row Count...", 0, bt)
        If dt.Rows.Count > 0 Then
            Dts.Log("Transfer Row Count > 0...", 0, bt)
            For Each row In dt.Rows
                Dim tblSourceName As String
                Dim tblSourceSchema As String

                tblSourceSchema = row("transferschema").ToString()
                tblSourceName = row("transferobject").ToString

                Select Case type
                    Case TransferObjectType.Table
                        xfr.ObjectList.Add(dbSource.Tables(tblSourceName.ToString, tblSourceSchema.ToString))
                    Case TransferObjectType.View
                        xfr.ObjectList.Add(dbSource.Views(tblSourceName.ToString, tblSourceSchema.ToString))
                    Case Else
                        Dts.Events.FireError(99, "Transfer Object", "Unknows object type", "", -1)
                        Dts.TaskResult = ScriptResults.Failure
                        'Throw New Exception("Unknown object transfer type.")
                End Select

            Next
            Dts.Events.FireInformation(1, "", "Just before transfering data", "", -1, False)

            xfr.TransferData()

        End If

    Catch ex As InternalSmoErrorException
        Dim bt(0) As Byte
        Dts.Events.FireError(99, "Transfer Objects", ex.Message.ToString(), "", -1)
        OutPutError(ex)
        Dts.TaskResult = ScriptResults.Failure

    End Try

End Sub

Private Sub OutPutError(ex As Exception)
    Dim ErrorLogDir As String
    Dim OutPutFileName As String
    ErrorLogDir = System.IO.Path.GetDirectoryName(Dts.Connections("ExportNightlyTransfer.xml").ConnectionString.ToString())
    OutPutFileName = ErrorLogDir + "\\" + Dts.Variables("PackageName").Value.ToString() + "ErrorReport" + Now.ToString("yyyyMMddHHmmss") + ".txt"
    Using sw As StreamWriter = New StreamWriter(OutPutFileName)
        sw.WriteLine(Now.ToString())
        sw.WriteLine("Package Name: " + Dts.Variables("PackageName").Value.ToString())
        sw.WriteLine("Task Name: " + Dts.Variables("TaskName").Value.ToString())
        sw.WriteLine(ex.Message.ToString())
        sw.WriteLine(ex.GetBaseException.ToString())
        sw.WriteLine(ex.StackTrace.ToString())
    End Using
End Sub

在此先感谢您的帮助。亚甲基

4

1 回答 1

0

通过添加 Catch innerException 代码,我能够获得有关该错误的特定信息。

  Catch ex As Exception
  Dts.Events.FireError(99, "", ex.Message.ToString(), "", -1)
  OutPutError(ex)

  If ex.InnerException IsNot Nothing Then
    Dts.Events.FireError(99, "", ex.InnerException.Message.ToString(), "", -1)
    OutPutError(ex.InnerException.Message.ToString())
  End If


  Dts.TaskResult = ScriptResults.Failure
  'MessageBox.Show("The transfer was aborted.")
End Try

问题解决了!!谢谢 :-)

于 2018-11-22T14:39:01.057 回答