如何在 VB.net 中调用 SQL Server 2000 DTS 包

发布于 2024-07-24 04:00:28 字数 29 浏览 6 评论 0原文

如何从 VB.net 应用程序调用 DTS?

How do I call a DTS from a VB.net application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩人痒 2024-07-31 04:00:28

如果您指的是 sql server 2000 中的数据转换服务(在 sql server 2000 中不可用)后来的版本,因为它被集成服务取代)那么有几种方法。

如果您的应用程序可以从 DTSRun 运行它在服务器上使用 process.start

process.start("dtsrun /S ""server name"" /U sa /N ""d:\task.dts""")

如果它不在服务器上,您仍然可以使用DTSRun 但在您使用 master.dbo.xp_cmdshell 调用的存储过程中,然后在代码中调用 SP。 (虽然如果你使用xp_cmdshell你一定要注意打开的安全风险)。

或者,您可以查看这篇文章,它解释了如何从 vb 脚本执行此操作,但代码可以轻松转换。:

https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11 -5164094.html

If you are referring to Data Transformation Services that is part of sql server 2000 (not available in later version as it was replaced with integration services) then there are a few ways.

You can run it from DTSRun if your app is on the server using process.start

process.start("dtsrun /S ""server name"" /U sa /N ""d:\task.dts""")

If it is not on the server you can still useDTSRun but inside a stored procedure that you call using master.dbo.xp_cmdshell, Then call the SP in your code. (Although if you use xp_cmdshell you must be aware of the security risks of opening up).

Alternatively you could look at this article it explains how to do it from vb script but the code could easily be converted.:

https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-5164094.html

-柠檬树下少年和吉他 2024-07-31 04:00:28

这是一组非常粗略的类,如果您希望通过 VB.Net 对 DTS 包进行编程控制,那么它应该能够满足您的需求。 我不保证它的正确性或可靠性,但它应该为您提供足够的入门指导。

只需将对 DTSPackage 对象 (COM) 库的引用添加到 Visual Studio 项目

Option Explicit On

选项 Strict On

Imports DTS
导入 System.Runtime.InteropServices

命名空间测试
公开课DTSRunner
私有 cpPoint 作为 ComTypes.IConnectionPoint
私有 cpContainer 作为 ComTypes.IConnectionPointContainer
私有 intCookie 作为整数
私有 PES 作为 PackageEventsSink

    Public Sub RunPackage(ByVal packageName As String)

        Dim pkg As New DTS.PackageClass
        pkg.LoadFromSQLServer("MySQLServer", String.Empty, String.Empty, DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, String.Empty, String.Empty, String.Empty, PackageName, System.Reflection.Missing.Value)

        cpContainer = CType(pkg, ComTypes.IConnectionPointContainer)
        cpContainer.FindConnectionPoint(New Guid("10020605-EB1C-11CF-AE6E-00AA004A34D5"), cpPoint)
        PES = New PackageEventsSink
        cpPoint.Advise(PES, intCookie)
        AddHandler PES.OnPackageOnError, AddressOf PackageErroredHandler
        AddHandler PES.OnPackageOnQueryCancel, AddressOf PackageQueryCancelHandler
        pkg.Execute()

        If cpPoint IsNot Nothing Then
            cpPoint.Unadvise(intCookie)
        End If
        If Not pkg Is Nothing Then
            pkg.UnInitialize()
        End If
        If PES IsNot Nothing Then
            RemoveHandler PES.OnPackageOnError, AddressOf PackageErroredHandler
            RemoveHandler PES.OnPackageOnQueryCancel, AddressOf PackageQueryCancelHandler
        End If
        PES = Nothing
        cpContainer = Nothing
        cpPoint = Nothing
        pkg = Nothing
    End Sub

    Private Sub PackageErroredHandler(ByVal sender As Object, ByVal e As DTSEventArgs)
        If Not packageErrored Then
            TraceEvent(TraceEventType.Error, -1, _
            "Error In DTS Package : {1} - {2} - {3} - {4}", _
            .EventSource, e.ErrorCode, e.Source, e.Description)
            packageErrored = True
        End If
        e.Cancel = False
    End Sub

    Private Sub PackageQueryCancelHandler(ByVal sender As Object, ByVal e As DTSEventArgs)
        e.Cancel = False
    End Sub


    Private Class DTSEventArgs
        Inherits EventArgs

        Public EventSource As String
        Public ErrorCode As Integer
        Public Source As String
        Public Description As String
        Public Cancel As Boolean
        Public PercentComplete As Integer = 0

        Public Sub New(ByVal EventSource As String, ByVal Source As String, ByVal Description As String, ByVal ErrorCode As Integer, ByVal Cancel As Boolean)
            Me.EventSource = EventSource
            Me.Source = Source
            Me.Description = Description
            Me.ErrorCode = ErrorCode
            Me.Cancel = Cancel
        End Sub

    End Class

    Private Delegate Sub PackageOnErrorEventHandler(ByVal sender As Object, ByVal e As DTSEventArgs)
    Private Delegate Sub PackageOnFinishEventHandler(ByVal sender As Object, ByVal e As DTSEventArgs)
    Private Delegate Sub PackageOnStartEventHandler(ByVal sender As Object, ByVal e As DTSEventArgs)
    Private Delegate Sub PackageOnProgressEventHandler(ByVal sender As Object, ByVal e As DTSEventArgs)
    Private Delegate Sub PackageOnQueryCancelEventHandler(ByVal sender As Object, ByVal e As DTSEventArgs)

    Private Class PackageEventsSink
        Implements DTS.PackageEvents

        Public Event OnPackageOnError As PackageOnErrorEventHandler
        Public Event OnPackageOnFinish As PackageOnFinishEventHandler
        Public Event OnPackageOnStart As PackageOnStartEventHandler
        Public Event OnPackageOnProgress As PackageOnProgressEventHandler
        Public Event OnPackageOnQueryCancel As PackageOnQueryCancelEventHandler

        Overridable Overloads Sub OnError(ByVal EventSource As String, _
        ByVal ErrorCode As Integer, ByVal Source As String, _
        ByVal Description As String, ByVal HelpFile As String, _
        ByVal HelpContext As Integer, ByVal IDofInterfaceWithError As String, _
        ByRef pbCancel As Boolean) Implements DTS.PackageEvents.OnError

            Dim e As New DTSEventArgs(EventSource, Source, Description, ErrorCode, pbCancel)
            TraceEvent(TraceEventType.Verbose, 0, String.Format(" OnError in {0}; ErrorCode = {1}, Source = {2}, Description = {3}, HelpFile = {4}, HelpContext = {5}, IDofInterfaceWithError = {6}", EventSource, ErrorCode, Source, Description, HelpFile, HelpContext, IDofInterfaceWithError))
            RaiseEvent OnPackageOnError(Me, e)
            pbCancel = e.Cancel

        End Sub

        Overridable Overloads Sub OnFinish(ByVal EventSource As String) Implements DTS.PackageEvents.OnFinish

            Dim e As New DTSEventArgs(EventSource, String.Empty, "Execution Finished", 0, False)
            TraceEvent(TraceEventType.Verbose, 0, String.Format(" OnFinish in {0}", EventSource))
            RaiseEvent OnPackageOnFinish(Me, e)

        End Sub

        Overridable Overloads Sub OnProgress(ByVal EventSource As String, _
        ByVal ProgressDescription As String, ByVal PercentComplete As Integer, _
        ByVal ProgressCountLow As Integer, ByVal ProgressCountHigh As Integer) _
        Implements DTS.PackageEvents.OnProgress

            Dim e As New DTSEventArgs(EventSource, String.Empty, ProgressDescription, 0, False)
            e.PercentComplete = PercentComplete
            TraceEvent(TraceEventType.Verbose, 0, String.Format(" OnProgress in {0}; ProgressDescription = {1}, PercentComplete = {2}, ProgressCountLow = {3}, ProgressCountHigh = {4}", EventSource, ProgressDescription, PercentComplete, ProgressCountLow, ProgressCountHigh))
            RaiseEvent OnPackageOnProgress(Me, e)

        End Sub


        Overridable Overloads Sub OnQueryCancel(ByVal EventSource As String, ByRef pbCancel As Boolean) Implements DTS.PackageEvents.OnQueryCancel

            Dim e As New DTSEventArgs(EventSource, String.Empty, String.Empty, 0, pbCancel)
            TraceEvent(TraceEventType.Verbose, 0, String.Format(" OnQueryCancel in {0}; pbCancel = {1}", EventSource, pbCancel))
            RaiseEvent OnPackageOnQueryCancel(Me, e)
            pbCancel = e.Cancel

        End Sub

        Overridable Overloads Sub OnStart(ByVal EventSource As String) Implements DTS.PackageEvents.OnStart

            Dim e As New DTSEventArgs(EventSource, String.Empty, "Execution Started", 0, False)
            TraceEvent(TraceEventType.Verbose, 0, String.Format(" OnStart in {0}", EventSource))
            RaiseEvent OnPackageOnStart(Me, e)

        End Sub

    End Class

        #End Region

End Class

End Namespace

This is a pretty crude set of classes that should do what you want if you want programmatic control over a DTS package from VB.Net. I make no guarantees over it's correctness or reliability but it should give you enough pointers to get started.

Just add a reference to the DTSPackage Object (COM) Library to your Visual Studio project

Option Explicit On

Option Strict On

Imports DTS
Imports System.Runtime.InteropServices

Namespace Test
Public Class DTSRunner
Private cpPoint As ComTypes.IConnectionPoint
Private cpContainer As ComTypes.IConnectionPointContainer
Private intCookie As Integer
Private PES As PackageEventsSink

    Public Sub RunPackage(ByVal packageName As String)

        Dim pkg As New DTS.PackageClass
        pkg.LoadFromSQLServer("MySQLServer", String.Empty, String.Empty, DTS.DTSSQLServerStorageFlags.DTSSQLStgFlag_UseTrustedConnection, String.Empty, String.Empty, String.Empty, PackageName, System.Reflection.Missing.Value)

        cpContainer = CType(pkg, ComTypes.IConnectionPointContainer)
        cpContainer.FindConnectionPoint(New Guid("10020605-EB1C-11CF-AE6E-00AA004A34D5"), cpPoint)
        PES = New PackageEventsSink
        cpPoint.Advise(PES, intCookie)
        AddHandler PES.OnPackageOnError, AddressOf PackageErroredHandler
        AddHandler PES.OnPackageOnQueryCancel, AddressOf PackageQueryCancelHandler
        pkg.Execute()

        If cpPoint IsNot Nothing Then
            cpPoint.Unadvise(intCookie)
        End If
        If Not pkg Is Nothing Then
            pkg.UnInitialize()
        End If
        If PES IsNot Nothing Then
            RemoveHandler PES.OnPackageOnError, AddressOf PackageErroredHandler
            RemoveHandler PES.OnPackageOnQueryCancel, AddressOf PackageQueryCancelHandler
        End If
        PES = Nothing
        cpContainer = Nothing
        cpPoint = Nothing
        pkg = Nothing
    End Sub

    Private Sub PackageErroredHandler(ByVal sender As Object, ByVal e As DTSEventArgs)
        If Not packageErrored Then
            TraceEvent(TraceEventType.Error, -1, _
            "Error In DTS Package : {1} - {2} - {3} - {4}", _
            .EventSource, e.ErrorCode, e.Source, e.Description)
            packageErrored = True
        End If
        e.Cancel = False
    End Sub

    Private Sub PackageQueryCancelHandler(ByVal sender As Object, ByVal e As DTSEventArgs)
        e.Cancel = False
    End Sub


    Private Class DTSEventArgs
        Inherits EventArgs

        Public EventSource As String
        Public ErrorCode As Integer
        Public Source As String
        Public Description As String
        Public Cancel As Boolean
        Public PercentComplete As Integer = 0

        Public Sub New(ByVal EventSource As String, ByVal Source As String, ByVal Description As String, ByVal ErrorCode As Integer, ByVal Cancel As Boolean)
            Me.EventSource = EventSource
            Me.Source = Source
            Me.Description = Description
            Me.ErrorCode = ErrorCode
            Me.Cancel = Cancel
        End Sub

    End Class

    Private Delegate Sub PackageOnErrorEventHandler(ByVal sender As Object, ByVal e As DTSEventArgs)
    Private Delegate Sub PackageOnFinishEventHandler(ByVal sender As Object, ByVal e As DTSEventArgs)
    Private Delegate Sub PackageOnStartEventHandler(ByVal sender As Object, ByVal e As DTSEventArgs)
    Private Delegate Sub PackageOnProgressEventHandler(ByVal sender As Object, ByVal e As DTSEventArgs)
    Private Delegate Sub PackageOnQueryCancelEventHandler(ByVal sender As Object, ByVal e As DTSEventArgs)

    Private Class PackageEventsSink
        Implements DTS.PackageEvents

        Public Event OnPackageOnError As PackageOnErrorEventHandler
        Public Event OnPackageOnFinish As PackageOnFinishEventHandler
        Public Event OnPackageOnStart As PackageOnStartEventHandler
        Public Event OnPackageOnProgress As PackageOnProgressEventHandler
        Public Event OnPackageOnQueryCancel As PackageOnQueryCancelEventHandler

        Overridable Overloads Sub OnError(ByVal EventSource As String, _
        ByVal ErrorCode As Integer, ByVal Source As String, _
        ByVal Description As String, ByVal HelpFile As String, _
        ByVal HelpContext As Integer, ByVal IDofInterfaceWithError As String, _
        ByRef pbCancel As Boolean) Implements DTS.PackageEvents.OnError

            Dim e As New DTSEventArgs(EventSource, Source, Description, ErrorCode, pbCancel)
            TraceEvent(TraceEventType.Verbose, 0, String.Format(" OnError in {0}; ErrorCode = {1}, Source = {2}, Description = {3}, HelpFile = {4}, HelpContext = {5}, IDofInterfaceWithError = {6}", EventSource, ErrorCode, Source, Description, HelpFile, HelpContext, IDofInterfaceWithError))
            RaiseEvent OnPackageOnError(Me, e)
            pbCancel = e.Cancel

        End Sub

        Overridable Overloads Sub OnFinish(ByVal EventSource As String) Implements DTS.PackageEvents.OnFinish

            Dim e As New DTSEventArgs(EventSource, String.Empty, "Execution Finished", 0, False)
            TraceEvent(TraceEventType.Verbose, 0, String.Format(" OnFinish in {0}", EventSource))
            RaiseEvent OnPackageOnFinish(Me, e)

        End Sub

        Overridable Overloads Sub OnProgress(ByVal EventSource As String, _
        ByVal ProgressDescription As String, ByVal PercentComplete As Integer, _
        ByVal ProgressCountLow As Integer, ByVal ProgressCountHigh As Integer) _
        Implements DTS.PackageEvents.OnProgress

            Dim e As New DTSEventArgs(EventSource, String.Empty, ProgressDescription, 0, False)
            e.PercentComplete = PercentComplete
            TraceEvent(TraceEventType.Verbose, 0, String.Format(" OnProgress in {0}; ProgressDescription = {1}, PercentComplete = {2}, ProgressCountLow = {3}, ProgressCountHigh = {4}", EventSource, ProgressDescription, PercentComplete, ProgressCountLow, ProgressCountHigh))
            RaiseEvent OnPackageOnProgress(Me, e)

        End Sub


        Overridable Overloads Sub OnQueryCancel(ByVal EventSource As String, ByRef pbCancel As Boolean) Implements DTS.PackageEvents.OnQueryCancel

            Dim e As New DTSEventArgs(EventSource, String.Empty, String.Empty, 0, pbCancel)
            TraceEvent(TraceEventType.Verbose, 0, String.Format(" OnQueryCancel in {0}; pbCancel = {1}", EventSource, pbCancel))
            RaiseEvent OnPackageOnQueryCancel(Me, e)
            pbCancel = e.Cancel

        End Sub

        Overridable Overloads Sub OnStart(ByVal EventSource As String) Implements DTS.PackageEvents.OnStart

            Dim e As New DTSEventArgs(EventSource, String.Empty, "Execution Started", 0, False)
            TraceEvent(TraceEventType.Verbose, 0, String.Format(" OnStart in {0}", EventSource))
            RaiseEvent OnPackageOnStart(Me, e)

        End Sub

    End Class

        #End Region

End Class

End Namespace
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文