Execute Sql Server 2005 Integration Services package from C#--zt

http://www.codeproject.com/KB/database/CallSSISFromCSharp.aspx

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.

Execute Sql Server 2005 Integration Services package from C#--zt

Execute Sql Server 2005 Integration Services package from C#--zt

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. 

Execute Sql Server 2005 Integration Services package from C#--zt

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.

Execute Sql Server 2005 Integration Services package from C#--zt Collapse
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

转载于:https://www.cnblogs.com/Nina-piaoye/archive/2008/09/23/1296745.html