Excecute SSIS package (DTSX) from C#

June 24, 2015 by Anuraj

.Net .Net 4.0 ASP.Net ASP.Net MVC SQL Server Visual Studio

SQL Server Integration Services (SSIS) is a component of the Microsoft SQL Server database software that can be used to perform a broad range of data migration tasks. SSIS is a platform for data integration and workflow applications.

You can execute SSIS package from C# using following snippet.

var application = new Application();
using (var package = application.LoadPackage(@"Package.dtsx", null))
{
    var execResult = package.Execute();
    Console.WriteLine(execResult.ToString());
}
Console.ReadKey();

This will execute the package and return results like Success or Failure. But in Failure scenarios, it won’t give any useful information for identifying the failure. You can implement an EventListener class to get more detailed execution information. Here is sample implementation.

class CustomEventListener : DefaultEvents
{
    public override bool OnError(DtsObject source, int errorCode, string subComponent,
        string description, string helpFile, int helpContext, string idofInterfaceWithError)
    {

        Console.WriteLine("Error in {0}/{1} : {2}", source, subComponent, description);
        return false;
    }
}

var eventListener = new CustomEventListener();
var application = new Application();
using (var package = application.LoadPackage(@"Package.dtsx", eventListener))
{
    var execResult = package.Execute(null, null, eventListener, null, null);
    Console.WriteLine(execResult.ToString());
}
Console.ReadKey();

Happy Programming :)

Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub