使用事件从VB.NET调用SSIS包

时间:2013-07-18 08:34:46

标签: vb.net ssis

这真的是一个问题和答案。我试图弄清楚如何从VB.NET中打开一个SSIS包,然后从包中公开事件,以便我可以检查进度。我在网上找不到任何答案。事实证明,这很简单。

1 个答案:

答案 0 :(得分:0)

这是Windows窗体代码:

Private Sub loadSSISPackage(ByVal packageFile As String)

    If file.Exists(packageFile) AndAlso packageFile.EndsWith(".dtsx") Then
        Dim pkg As New Microsoft.SqlServer.Dts.Runtime.Package
        Dim app As New Microsoft.SqlServer.Dts.Runtime.Application
        Dim pkgResults As Microsoft.SqlServer.Dts.Runtime.DTSExecResult


        pkg = app.LoadPackage(packageFile, Nothing)

        Dim pkgEvents As New PackageEvents


        pkgResults = pkg.Execute(Nothing, Nothing, pkgEvents, Nothing, Nothing)
        MsgBox(pkgResults.ToString())
    Else
        Environment.Exit(-1)
    End If
End Sub

这是我创建的课程:

公共类PackageEvents     实现Microsoft.SqlServer.Dts.Runtime.IDTSEvents

Public Sub OnBreakpointHit(breakpointSite As Microsoft.SqlServer.Dts.Runtime.IDTSBreakpointSite, breakpointTarget As Microsoft.SqlServer.Dts.Runtime.BreakpointTarget) Implements Microsoft.SqlServer.Dts.Runtime.IDTSEvents.OnBreakpointHit

End Sub

Public Sub OnCustomEvent(taskHost As Microsoft.SqlServer.Dts.Runtime.TaskHost, eventName As String, eventText As String, ByRef arguments() As Object, subComponent As String, ByRef fireAgain As Boolean) Implements Microsoft.SqlServer.Dts.Runtime.IDTSEvents.OnCustomEvent

End Sub

Public Function OnError(source As Microsoft.SqlServer.Dts.Runtime.DtsObject, errorCode As Integer, subComponent As String, description As String, helpFile As String, helpContext As Integer, idofInterfaceWithError As String) As Boolean Implements Microsoft.SqlServer.Dts.Runtime.IDTSEvents.OnError

End Function

Public Sub OnExecutionStatusChanged(exec As Microsoft.SqlServer.Dts.Runtime.Executable, newStatus As Microsoft.SqlServer.Dts.Runtime.DTSExecStatus, ByRef fireAgain As Boolean) Implements Microsoft.SqlServer.Dts.Runtime.IDTSEvents.OnExecutionStatusChanged

End Sub

Public Sub OnInformation(source As Microsoft.SqlServer.Dts.Runtime.DtsObject, informationCode As Integer, subComponent As String, description As String, helpFile As String, helpContext As Integer, idofInterfaceWithError As String, ByRef fireAgain As Boolean) Implements Microsoft.SqlServer.Dts.Runtime.IDTSEvents.OnInformation
    Debug.Print(CStr(informationCode))
    Debug.Print(CStr(subComponent))
    Debug.Print(CStr(description))
    Debug.Print(CStr(helpFile))
    Debug.Print(CStr(helpContext))


End Sub

Public Sub OnPostExecute(exec As Microsoft.SqlServer.Dts.Runtime.Executable, ByRef fireAgain As Boolean) Implements Microsoft.SqlServer.Dts.Runtime.IDTSEvents.OnPostExecute

End Sub

Public Sub OnPostValidate(exec As Microsoft.SqlServer.Dts.Runtime.Executable, ByRef fireAgain As Boolean) Implements Microsoft.SqlServer.Dts.Runtime.IDTSEvents.OnPostValidate

End Sub

Public Sub OnPreExecute(exec As Microsoft.SqlServer.Dts.Runtime.Executable, ByRef fireAgain As Boolean) Implements Microsoft.SqlServer.Dts.Runtime.IDTSEvents.OnPreExecute

End Sub

Public Sub OnPreValidate(exec As Microsoft.SqlServer.Dts.Runtime.Executable, ByRef fireAgain As Boolean) Implements Microsoft.SqlServer.Dts.Runtime.IDTSEvents.OnPreValidate

End Sub

Public Sub OnProgress(taskHost As Microsoft.SqlServer.Dts.Runtime.TaskHost, progressDescription As String, percentComplete As Integer, progressCountLow As Integer, progressCountHigh As Integer, subComponent As String, ByRef fireAgain As Boolean) Implements Microsoft.SqlServer.Dts.Runtime.IDTSEvents.OnProgress


    'Debug.Print(progressDescription)
    'Debug.Print(CStr(taskHost.ExecutionStatus))
    'Debug.Print(CStr(percentComplete))
    'Debug.Print(CStr(progressCountLow))
    'Debug.Print(CStr(progressCountHigh))

End Sub

Public Function OnQueryCancel() As Boolean Implements Microsoft.SqlServer.Dts.Runtime.IDTSEvents.OnQueryCancel

End Function

Public Sub OnTaskFailed(taskHost As Microsoft.SqlServer.Dts.Runtime.TaskHost) Implements Microsoft.SqlServer.Dts.Runtime.IDTSEvents.OnTaskFailed

End Sub

Public Sub OnVariableValueChanged(DtsContainer As Microsoft.SqlServer.Dts.Runtime.DtsContainer, variable As Microsoft.SqlServer.Dts.Runtime.Variable, ByRef fireAgain As Boolean) Implements Microsoft.SqlServer.Dts.Runtime.IDTSEvents.OnVariableValueChanged

End Sub

Public Sub OnWarning(source As Microsoft.SqlServer.Dts.Runtime.DtsObject, warningCode As Integer, subComponent As String, description As String, helpFile As String, helpContext As Integer, idofInterfaceWithError As String) Implements Microsoft.SqlServer.Dts.Runtime.IDTSEvents.OnWarning

End Sub

结束班

请注意,一旦编码工具线,所有子将自动出现。现在可以捕获所有SSIS包事件了!

关于在单独的线程上进行交叉编组显然会有一些工作要做,以便将SSIS事件信息提供给表单,但那是另一个故事......