Monday, May 27, 2013

run external package with custom listener

Microsoft.SqlServer.Dts.Runtime.Application app = new Microsoft.SqlServer.Dts.Runtime.Application();
CustEventListener eventListener = new CustEventListener(Dts);
// Load package
string packageName = "test.dtsx";
Package childPackage = app.LoadPackage(packageName, eventListener);
// Execute
DTSExecResult dtsResult = childPackage.Execute(null, null, eventListener, null, null);
if (dtsResult == DTSExecResult.Success)
{
    Log("Success running child package {0}", packageName);
}
  ...

public class CustEventListener : Microsoft.SqlServer.Dts.Runtime.DefaultEvents
{
    Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel dts;
    public CustEventListener(Microsoft.SqlServer.Dts.Tasks.ScriptTask.ScriptObjectModel dts)
    {
        this.dts = dts;
    }

    public override bool OnError(DtsObject source, int errorCode,string subComponent, string description, string helpFile,int helpContext, string idofInterfaceWithError)
    {
        dts.Events.FireError(errorCode, subComponent, description, helpFile, helpContext);
        return false;    
    }

    public override void OnInformation(DtsObject source, int informationCode,string subComponent, string description, string helpFile,int helpContext, string idofInterfaceWithError, ref bool fireAgain)
    {
        dts.Events.FireInformation(informationCode, subComponent, description, helpFile, helpContext, ref fireAgain);

    }

    public override void OnWarning(DtsObject source, int warningCode,string subComponent, string description, string helpFile,int helpContext, string idofInterfaceWithError)
    {
        dts.Events.FireWarning(warningCode, subComponent, description, helpFile, helpContext);
    }
}

No comments:

Post a Comment