SQL Server 2008 Books Online (October 2009)
Loading and Running a Local Package Programmatically

You can run Integration Services packages as needed or at predetermined times by using the methods described in Running Packages. However, with only a few lines of code, you can also run a package from a custom application such as a Windows Forms application, a console application, an ASP.NET Web form or Web service, or a Windows service.

This topic discusses:

  • Loading a package programmatically
  • Running a package programmatically

All of the methods used in this topic to load and run packages require a reference to the Microsoft.SqlServer.ManagedDTS assembly. After adding the reference in a new project, import the Microsoft.SqlServer.Dts.Runtime namespace with a using or Imports statement.

Loading a Package Programmatically

To load a package programmatically on the local computer, whether the package is stored locally or remotely, call one of the following methods:

Storage Location Method to Call

File

LoadPackage

or

LoadPackage

SSIS Package Store

LoadFromDtsServer

SQL Server

LoadFromSqlServer

ms136090.note(en-us,SQL.100).gifImportant:
The methods of the Application class for working with the SSIS Package Store only support ".", localhost, or the server name for the local server. You cannot use "(local)".

Running a Package Programmatically

Developing a custom application in managed code that runs a package on the local computer requires the following approach. The steps summarized here are demonstrated in the sample console application that follows.

To run a package on the local computer programmatically

  1. Start the Visual Studio development environment, and create a new application in your preferred development language. This example uses a console application; however you can also run a package from a Windows Forms application, an ASP.NET Web form or Web service, or a Windows service.

  2. On the Project menu, click Add Reference and add a reference to Microsoft.SqlServer.ManagedDTS.dll. Click OK.

  3. Use the Visual Basic Imports statement or the C# using statement to import the Microsoft.SqlServer.Dts.Runtime namespace.

  4. Add the following code in the main routine. The completed console application should look like the following example.

    ms136090.note(en-us,SQL.100).gifNote:
    The sample code demonstrates loading the package from the file system by using the LoadPackage method. However you can also load the package from the MSDB database by calling the LoadFromSqlServer method, or from the Integration Services package store by calling the LoadFromDtsServer method.

  5. Run the project. The sample code executes the CalculatedColumns sample package that is installed with the SQL Server samples. The result of package execution is displayed in the console window.

Sample Code

VisualBasic
Imports Microsoft.SqlServer.Dts.Runtime

Module Module1

  Sub Main()

    Dim pkgLocation As String
    Dim pkg As New Package
    Dim app As New Application
    Dim pkgResults As DTSExecResult

    pkgLocation = _
      "C:\Program Files\Microsoft SQL Server\100\Samples\Integration Services" & _
      "\Package Samples\CalculatedColumns Sample\CalculatedColumns\CalculatedColumns.dtsx"
    pkg = app.LoadPackage(pkgLocation, Nothing)
    pkgResults = pkg.Execute()

    Console.WriteLine(pkgResults.ToString())
    Console.ReadKey()

  End Sub

End Module
CSharp
using System;
using Microsoft.SqlServer.Dts.Runtime;

namespace RunFromClientAppCS
{
  class Program
  {
    static void Main(string[] args)
    {
      string pkgLocation;
      Package pkg;
      Application app;
      DTSExecResult pkgResults;

      pkgLocation =
        @"C:\Program Files\Microsoft SQL Server\100\Samples\Integration Services" +
        @"\Package Samples\CalculatedColumns Sample\CalculatedColumns\CalculatedColumns.dtsx";
      app = new Application();
      pkg = app.LoadPackage(pkgLocation, null);
      pkgResults = pkg.Execute();

      Console.WriteLine(pkgResults.ToString());
      Console.ReadKey();
    }
  }
}
Capturing Events from a Running Package

When you run a package programmatically as shown in the preceding sample, you may also want to capture errors and other events that occur as the package executes. You can accomplish this by adding a class that inherits from the DefaultEvents class, and by passing a reference to that class when you load the package. Although the following example captures only the OnError event, there are many other events that the DefaultEvents class lets you capture.

To run a package on the local computer programmatically and capture package events

  1. Follow the steps in the preceding example to create a project for this example.

  2. Add the following code in the main routine. The completed console application should look like the following example.

  3. Run the project. The sample code executes the CalculatedColumns sample package that is installed with the SQL Server samples. The result of package execution is displayed in the console window, along with any errors that occur.

Sample Code

VisualBasic
Imports Microsoft.SqlServer.Dts.Runtime

Module Module1

  Sub Main()

    Dim pkgLocation As String
    Dim pkg As New Package
    Dim app As New Application
    Dim pkgResults As DTSExecResult

    Dim eventListener As New EventListener()

    pkgLocation = _
      "C:\Program Files\Microsoft SQL Server\100\Samples\Integration Services" & _
      "\Package Samples\CalculatedColumns Sample\CalculatedColumns\CalculatedColumns.dtsx"
    pkg = app.LoadPackage(pkgLocation, eventListener)
    pkgResults = pkg.Execute(Nothing, Nothing, eventListener, Nothing, Nothing)

    Console.WriteLine(pkgResults.ToString())
    Console.ReadKey()

  End Sub

End Module

Class EventListener
  Inherits DefaultEvents

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

    ' Add application–specific diagnostics here.
    Console.WriteLine("Error in {0}/{1} : {2}", source, subComponent, description)
    Return False

  End Function

End Class
CSharp
using System;
using Microsoft.SqlServer.Dts.Runtime;

namespace RunFromClientAppWithEventsCS
{
  class MyEventListener : DefaultEvents
  {
    public override bool OnError(DtsObject source, int errorCode, string subComponent, 
      string description, string helpFile, int helpContext, string idofInterfaceWithError)
    {
      // Add application-specific diagnostics here.
      Console.WriteLine("Error in {0}/{1} : {2}", source, subComponent, description);
      return false;
    }
  }
  class Program
  {
    static void Main(string[] args)
    {
      string pkgLocation;
      Package pkg;
      Application app;
      DTSExecResult pkgResults;

      MyEventListener eventListener = new MyEventListener();

      pkgLocation =
        @"C:\Program Files\Microsoft SQL Server\100\Samples\Integration Services" +
        @"\Package Samples\CalculatedColumns Sample\CalculatedColumns\CalculatedColumns.dtsx";
      app = new Application();
      pkg = app.LoadPackage(pkgLocation, eventListener);
      pkgResults = pkg.Execute(null, null, eventListener, null, null);

      Console.WriteLine(pkgResults.ToString());
      Console.ReadKey();
    }
  }
}

Integration Services icon (small)  Stay Up to Date with Integration Services

For the latest downloads, articles, samples, and videos from Microsoft, as well as selected solutions from the community, visit the Integration Services page on MSDN or TechNet:

For automatic notification of these updates, subscribe to the RSS feeds available on the page.

See Also

Tasks

Loading and Running a Remote Package Programmatically
Loading the Output of a Local Package

Concepts

Understanding the Differences between Local and Remote Execution

Help and Information

Getting SQL Server 2008 Assistance
Tags :


Community Content

Thomas Lee
package is running but transformation is not succeed

Hi

I have used abone code, its compiling while running its show succes message, but transformation is not happen.

but same code i have tried with sql 2005 package, its running fine.

Regards

Kapil M Gupta
Gupta_kapil@satyam.com

[Tai Yee - MSFT] Hi, the best way to get questions answered is to post in the Microsoft Community Forum. The SQL Server Forums are located here: http://forums.microsoft.com/MSDN/default.aspx?ForumGroupID=19&SiteID=1 . You will want to post this to the Transact-SQL forum.


Thomas Lee
Experiencing the same issue.

I have experienced the same issue. I'm getting a success message but nothing happens.

[tfl - 21 05 09] Hi - and thanks for your post. You should post questions like this to the MSDN Forums at http://forums.microsoft.com/msdn or the MSDN Newsgroups at http://www.microsoft.com/communities/newsgroups/en-us/. You are much more likely get a quicker response using the forums than through the Community Content. 

For specific help about:
Visual Studio : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.vstudio%2C&
.NET Framework : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public.dotnet.framework
All Public : http://groups.google.com/groups/dir?sel=usenet%3Dmicrosoft.public%2C&
 

Druzil
Working Directory
Is there a way to change the working directory? If the configuration file is specified as relative to the package then it isn't found when executed thus causing the package to run with incorrect config settings.
Tags :

Jeff Reed
Missing...Microsoft.SqlServer.ManagedDTS
Why don't I see this in my list? I have VS2008 and SQL Server installed. Do I have to browse for it? If so, where is it and what's the filename?

Thanks, Jeff
Tags :

Page view tracker