如何从 C# 中加载的 SSIS 包捕获事件?

发布于 2024-07-10 07:10:55 字数 902 浏览 11 评论 0 原文

我有一个执行多项任务的 SSIS 包。 我在 Business Intelligence Studio 2005 中手动添加了 OnExecStatusChanged 事件的包级别的事件处理程序。

我的问题是,如何在 C# 中添加此事件的处理程序? 我已按照此处的指示加载了包 我还创建了一个继承自 Microsoft.SqlServer.Dts.Runtime.DefaultEvents 的自定义类,它是我的“监听器”:

    Microsoft.SqlServer.Dts.Runtime.SqlPackageEventListener sqlListener = new SqlPackageEventListener();
    Microsoft.SqlServer.Dts.Runtime.Application sqlPackageLoader = new Application();
    Microsoft.SqlServer.Dts.Runtime.Package sqlPackage = sqlPackageLoader.LoadPackage(@"path\MigrateData.dtsx", sqlListener);
    sqlPackage.Execute(null, null, sqlListener, null, null);

如果我检查 sqlPackage.EventHandlers.Count 属性,我会得到在商业智能中添加的处理程序的正确编号工作室。

有没有办法在 C# 中处理这些事件?

谢谢。

I have an SSIS package that executes several tasks. I manually added an event handler inside Business Intelligence Studio 2005 at the package level for the OnExecStatusChanged event.

My question is, how can I add a handler for this event inside C#? I have loaded the package as pointed here and I also created a custom class inherited from Microsoft.SqlServer.Dts.Runtime.DefaultEvents which is my "Listener":

    Microsoft.SqlServer.Dts.Runtime.SqlPackageEventListener sqlListener = new SqlPackageEventListener();
    Microsoft.SqlServer.Dts.Runtime.Application sqlPackageLoader = new Application();
    Microsoft.SqlServer.Dts.Runtime.Package sqlPackage = sqlPackageLoader.LoadPackage(@"path\MigrateData.dtsx", sqlListener);
    sqlPackage.Execute(null, null, sqlListener, null, null);

If I check the sqlPackage.EventHandlers.Count property I get the right number for handlers added inside Business Intelligence Studio.

Is there some way to handle those events inside C#?

Thanks.

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

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

发布评论

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

评论(1

欢你一世 2024-07-17 07:10:55

好吧,我没有找到任何东西,所以我想出了一个解决方法,这样我就会自动回复我:

由于没有办法直接捕获 SSIS 包生成的事件,所以我在 listener

public class SqlPackageEventListener : DefaultEvents
{
    public SqlPackageChangedHandler OnPackageError;

    public override bool OnError(DtsObject source, int errorCode, string subComponent, string description, string helpFile, int helpContext, string idofInterfaceWithError) {
        OnPackageError(this, new PackageErrorEventArgs(source, subComponent, description));
        return base.OnError(source, errorCode, subComponent, description, helpFile, helpContext, idofInterfaceWithError);
    }

    public delegate void SqlPackageChangedHandler(
        object sqlPackage,
        EventArgs packageInfo
        );
}

public class PackageErrorEventArgs : EventArgs 
{
    private DtsObject source;
    public DtsObject Source {
        get { return source; }
        set { source = value; }
    }

    private string subcomponent;
    public string Subcomponent {
        get { return subcomponent; }
        set { subcomponent = value; }
    }

    private string description;
    public string Description {
        get { return description; }
        set { description = value; }
    }

    public PackageErrorEventArgs(DtsObject source, string subcomponent, string description) {
        this.description = description;
        this.source = source;
        this.subcomponent = subcomponent;
    }
}

public class Test 
{
    SqlPackageEventListener sqlListener = new SqlPackageEventListener();
    sqlListener.OnPackageError += new SqlPackageEventListener.SqlPackageChangedHandler(sqlListener_OnPackageError);
    Microsoft.SqlServer.Dts.Runtime.Application sqlPackageLoader = new Microsoft.SqlServer.Dts.Runtime.Application();
    Microsoft.SqlServer.Dts.Runtime.Package sqlPackage = Microsoft.SqlServer.Dts.Runtime.sqlPackageLoader.LoadPackage(@"path_to\file.dtsx", sqlListener);
    sqlPackage.Execute(null, null, sqlListener, null, null)

    public void sqlListener_OnPackageError(object sender, EventArgs args) {
        //code to handle the event
    }
} 

因此“技巧”是将委托添加到“Listener”对象您将其传递给 Package 对象的 LoadPackage 方法,这样,我们就可以访问侦听器内的“OnError”重写并引发事件。 正如您所看到的,我实现了自己的 EventArguments 类,因此我们可以将重要数据传递给处理程序代码,并查看正在运行的包或从 DefaultEvents 继承时重写方法可以获得的任何其他信息。

当然,我在这里只实现了 OnError,您可以实现 SQL Server 支持的任何其他您喜欢的处理程序,并且可以覆盖它,因为这是我们引发事件的范围。

这样我就可以创建我的 SqlPackageEventListener 对象并使用 sqlListener_OnPackageError 方法处理它的“OnPackageError”事件,并在 SSIS 包的执行导致任何错误时执行我需要的操作。

Well I did not find anything so I came up with a work around so I will auto-respond to me:

Since there is no way to directly catch the events that the SSIS package make then I implemented my own events inside my listener:

public class SqlPackageEventListener : DefaultEvents
{
    public SqlPackageChangedHandler OnPackageError;

    public override bool OnError(DtsObject source, int errorCode, string subComponent, string description, string helpFile, int helpContext, string idofInterfaceWithError) {
        OnPackageError(this, new PackageErrorEventArgs(source, subComponent, description));
        return base.OnError(source, errorCode, subComponent, description, helpFile, helpContext, idofInterfaceWithError);
    }

    public delegate void SqlPackageChangedHandler(
        object sqlPackage,
        EventArgs packageInfo
        );
}

public class PackageErrorEventArgs : EventArgs 
{
    private DtsObject source;
    public DtsObject Source {
        get { return source; }
        set { source = value; }
    }

    private string subcomponent;
    public string Subcomponent {
        get { return subcomponent; }
        set { subcomponent = value; }
    }

    private string description;
    public string Description {
        get { return description; }
        set { description = value; }
    }

    public PackageErrorEventArgs(DtsObject source, string subcomponent, string description) {
        this.description = description;
        this.source = source;
        this.subcomponent = subcomponent;
    }
}

public class Test 
{
    SqlPackageEventListener sqlListener = new SqlPackageEventListener();
    sqlListener.OnPackageError += new SqlPackageEventListener.SqlPackageChangedHandler(sqlListener_OnPackageError);
    Microsoft.SqlServer.Dts.Runtime.Application sqlPackageLoader = new Microsoft.SqlServer.Dts.Runtime.Application();
    Microsoft.SqlServer.Dts.Runtime.Package sqlPackage = Microsoft.SqlServer.Dts.Runtime.sqlPackageLoader.LoadPackage(@"path_to\file.dtsx", sqlListener);
    sqlPackage.Execute(null, null, sqlListener, null, null)

    public void sqlListener_OnPackageError(object sender, EventArgs args) {
        //code to handle the event
    }
} 

So the "trick" is to add a delegate to your "Listener" object that you pass to the LoadPackage method of the Package object, that way, we can access the "OnError" override inside the listener and raise the event. As you can see I implemented my own EventArguments class so we can pass important data to our handler code and see what package is running or any other information that you can get from overriding the methods when you inherit from DefaultEvents.

Of course I only implemented OnError here, you can implement any other handler you like that is supported by SQL Server and that can be overrided since that is the scope where we raise the event.

That way I can create my SqlPackageEventListener object and handle it's "OnPackageError" event with the sqlListener_OnPackageError method and do whatever I need in case of any error that the execution of the SSIS package caused.

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