5,316,870 members and growing! (16,377 online)
Email Password   helpLost your password?
Database » Database » General     Beginner

Execute Sql Server 2005 Integration Services package from C#

By Jeff Modzel

Learn how to load and execute a Sql Server 2005 Integration Services package from C# code
C#, SQL, Windows, .NET, Visual Studio, SQL 2005, SQL Server, Architect, DBA, Dev

Posted: 25 May 2006
Updated: 25 May 2006
Views: 74,842
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 4.81 Rating: 4.20 out of 5
2 votes, 14.3%
1
1 vote, 7.1%
2
2 votes, 14.3%
3
4 votes, 28.6%
4
5 votes, 35.7%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

There are many ways to execute a Sql Server 2005 Integration Services (SSIS) package.  You can use the command line utility DTExec.exe or its window equivalent, DTExecUI.exe.  A package can be executed within Sql Server Business Intelligence Studio (Visual Studio) or from a Sql Server Agent Job Step.  A package can also be executed from .NET code!

Example SSIS Package

For the purposes of demonstration, I created a simple package that takes some data out of a Sql Server AdvantureWorks database and dumps it into a flatfile.  The package also has one variable.

Sample screenshot

Sample screenshot

In any kind of "real world" scenario, your package will usually be driven by a configuration file.  A SSIS configuration file is an XML file with a .dtsConfig extension that contains settings you can apply to a package (without actually changing or editing the package).  In my example files, you can edit the configuration file with your Sql Server and flat file connection information and run the package.  You'll never have to edit the actual package file.  Here's a very good tutorial on configuration file syntax.  Configurations can also be stored in a Sql Server table, but I won't cover that here.

Let's start coding...

You need to add a referrence to Microsoft.SQLServer.ManagedDTS.dll.  I believe that this dll is only installed on a machine that has Sql Server components installed. 

Sample screenshot

The amount of code to execute a SSIS package is surprisingly small and concise.  Notice that I added a using directive for the Microsoft.SqlServer.Dts.Runtime namespace.

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Dts.Runtime;

namespace ExecuteSSIS
{
  class Program
  {
    static void Main(string[] args)
      {
        Application app = new Application();
        //
        // Load package from file system
        //
        Package package = app.LoadPackage("c:\\ExamplePackage.dtsx", null);
        package.ImportConfigurationFile("c:\\ExamplePackage.dtsConfig");
        Variables vars = package.Variables;
        vars["MyVariable"].Value = "value from c#";

        DTSExecResult result = package.Execute();

        Console.WriteLine("Package Execution results: {0}",result.ToString());

        //
        // Load package from Sql Server
        //
        Package package2 = app.LoadFromSqlServer(
            "ExamplePackage","server_name", "sa", "your_password", null);

        package2.ImportConfigurationFile("c:\\ExamplePackage.dtsConfig");
        Variables vars2 = package2.Variables;
        vars2["MyVariable"].Value = "value from c# again";

        DTSExecResult result2 = package2.Execute();

        Console.WriteLine("Package Execution results: {0}", 
             result2.ToString());


     }

  }

 }

First, you create an Application object, which provides access to the DTS (Integration Services) runtime.  Then you use the Application object to load a packge from either the file system or from Sql Server, I've demonstrated both. Once you have the package loaded into a Package object, you call the ImportConfigurationFile() method to load and apply the configuration file to the package. The Package object also has a Variables collection that provides access to the package's variable. Finally, to actually execute a package, call the Execute() method.

Conclusion

This article was meant to quickly demonstrate how to load and execute a package.  There is much, much more you can do with the managed DTS namespace.  There are objects and additional namespaces that allow you to load and inspect packages or even create new packages from .NET code.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jeff Modzel


Technical Architect
Sungard HE
Occupation: Web Developer
Location: United States United States

Other popular Database articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 24 of 24 (Total in Forum: 24) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralAnyone have the code to do this in VB?memberREBMCAD9:18 10 Jul '08  
GeneralIt's very good!But how about the excel files?memberChaney.Wu1:25 12 Mar '08  
GeneralGood articlememberMuaddubby7:01 3 Mar '08  
Generalhelp needed in case of ssis package..membertasumisra2:02 11 Dec '07  
GeneralRe: help needed in case of ssis package..memberJames_Isisw22:56 15 Dec '07  
QuestionSSiS Problem Urgent!!memberPapai.T23:29 10 Oct '07  
GeneralRun SSIS package from C# using Windows authenticationmemberAlisterN16:38 5 Aug '07  
QuestionExecuting SSIS package always gets failurememberBrillientking20:31 3 Jul '07  
AnswerRe: Executing SSIS package always gets failuremembersaom12:15 6 Jul '07  
GeneralWhat if Configuration is type DTSConfigurationType.SqlServermemberJMB122812:07 9 May '07  
QuestionError creating applicationmembercibercharlie5:44 9 May '07  
GeneralWhat about calling a package from ASP.NETmemberFatihSahin14:27 19 Dec '06  
AnswerRe: What about calling a package from ASP.NETmemberchristhejackal6:28 12 Feb '07  
GeneralRe: What about calling a package from ASP.NETmemberABlokha772:56 18 May '07  
GeneralOutstanding!memberWbehning13:44 6 Nov '06  
GeneralThanks.memberSlackshot6:54 16 Oct '06  
GeneralThanksmemberFergal Boden1:07 7 Sep '06  
General[OLE DB Destination [306]] Error: The AcquireConnection method call to the connection manager failed with error code 0xC0202009.memberBalayc2:11 29 Jul '06  
GeneralWrong referencesmembervavigli11:38 20 Jul '06  
QuestionRun Time Error While Execute SSIS Package from C#(Windows Application)memberdeepumi16:31 5 Jun '06  
AnswerRe: Run Time Error While Execute SSIS Package from C#(Windows Application)memberJeff Modzel4:58 6 Jun '06  
GeneralPassing global variables back to C# from SSISmemberNigel-Findlater21:53 30 May '06  
GeneralRe: Passing global variables back to C# from SSISmemberJeff Modzel3:43 31 May '06  
AnswerRe: Passing global variables back to C# from SSISmemberJeff Modzel8:44 1 Jun '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 25 May 2006
Editor:
Copyright 2006 by Jeff Modzel
Everything else Copyright © CodeProject, 1999-2008
Web20 | Advertise on the Code Project