【问题标题】:How to execute an SSIS package from .NET?如何从 .NET 执行 SSIS 包?
【发布时间】:2008-11-07 21:48:37
【问题描述】:

我有一个 SSIS 包,最终我也想传递参数,这些参数将来自 .NET 应用程序(VB 或 C#),所以我很好奇是否有人知道如何执行此操作,或者更好的是网站提供有关如何操作的有用提示。

所以基本上我想从 .NET 执行一个 SSIS 包,传递它可以在其中使用的 SSIS 包参数。

例如,SSIS 包将使用平面文件导入 SQL 数据库,但文件的路径和名称可能是从 .Net 应用程序传递的参数。

【问题讨论】:

  • 致未来读者:在使用以下解决方案之前,请检查您的许可。我相信这只适用于安装了 SSIS 的机器,而不仅仅是 DLL 参考。在生产环境中,通常即使安装 SSIS 而不安装数据库引擎本身也需要许可证。
  • 谁能确认@JohnSpiegel 的评论?如果安装了 SSIS,这是否仅适用于生产环境?
  • 仅供参考,以编程方式运行 SSIS 包的链接已更改为 docs.microsoft.com/en-us/archive/blogs/michen/…

标签: c# .net sql-server vb.net ssis


【解决方案1】:

这里是如何从代码中设置包中的变量 -

using Microsoft.SqlServer.Dts.Runtime;

private void Execute_Package()
    {           
        string pkgLocation = @"c:\test.dtsx";

        Package pkg;
        Application app;
        DTSExecResult pkgResults;
        Variables vars;

        app = new Application();
        pkg = app.LoadPackage(pkgLocation, null);

        vars = pkg.Variables;
        vars["A_Variable"].Value = "Some value";               

        pkgResults = pkg.Execute(null, vars, null, null, null);

        if (pkgResults == DTSExecResult.Success)
            Console.WriteLine("Package ran successfully");
        else
            Console.WriteLine("Package failed");
    }

【讨论】:

  • @IanCampbell 我假设您指的是 Microsoft.SqlServer.Dts.Runtime? Dts 只是 SSIS 的旧名称 - 它只是名称空间声明。上面的代码将得到支持。
  • @IanCampbell 是的,DTS 已被贬低(事实上,我认为您甚至不能将 DTS 与最新版本的 SQL Server 一起使用 - 不是我试图找到它!)。但是,包含一些 SSIS 组件的 .Net 命名空间仍然包含 Dts 字。我向你保证这是当前版本并且有效。
  • 好的,谢谢@Spikeh!值得注意的是,当我最近实现了类似的代码来使用 Dts 加载 SSIS 包时,我必须手动从 C:\Windows\assembly 文件夹中的“GAC”中获取 Microsoft.SqlServer.ManagedDTS.dll 文件来编译此类代码。
  • 是的,我也是——我昨天也是这样!我正在使用 VS2012 和 .Net 4(用于 SSIS 包)/4.5(用于我的单元测试)。我必须从 C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SqlServer.ManagedDTS\v4.0_11.0.0.0__89845dcd8080cc91 获取程序集,因为它不存在于任何其他程序集文件夹或SQL 文件夹。
  • MSDN 的一些链接: 1) 本地包(同一台机器):msdn.microsoft.com/en-us/library/ms136090.aspx。 2) 远程包(存储在程序运行所在机器以外的机器上),使用 SQL 代理作业:msdn.microsoft.com/en-us/library/ms403355.aspx
【解决方案2】:

以下是使用 SQL Server 2012 引入的 SSDB 目录的方法...

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.SqlClient;

using Microsoft.SqlServer.Management.IntegrationServices;

public List<string> ExecutePackage(string folder, string project, string package)
{
    // Connection to the database server where the packages are located
    SqlConnection ssisConnection = new SqlConnection(@"Data Source=.\SQL2012;Initial Catalog=master;Integrated Security=SSPI;");

    // SSIS server object with connection
    IntegrationServices ssisServer = new IntegrationServices(ssisConnection);

    // The reference to the package which you want to execute
    PackageInfo ssisPackage = ssisServer.Catalogs["SSISDB"].Folders[folder].Projects[project].Packages[package];

    // Add a parameter collection for 'system' parameters (ObjectType = 50), package parameters (ObjectType = 30) and project parameters (ObjectType = 20)
    Collection<PackageInfo.ExecutionValueParameterSet> executionParameter = new Collection<PackageInfo.ExecutionValueParameterSet>();

    // Add execution parameter (value) to override the default asynchronized execution. If you leave this out the package is executed asynchronized
    executionParameter.Add(new PackageInfo.ExecutionValueParameterSet { ObjectType = 50, ParameterName = "SYNCHRONIZED", ParameterValue = 1 });

    // Add execution parameter (value) to override the default logging level (0=None, 1=Basic, 2=Performance, 3=Verbose)
    executionParameter.Add(new PackageInfo.ExecutionValueParameterSet { ObjectType = 50, ParameterName = "LOGGING_LEVEL", ParameterValue = 3 });

    // Add a project parameter (value) to fill a project parameter
    executionParameter.Add(new PackageInfo.ExecutionValueParameterSet { ObjectType = 20, ParameterName = "MyProjectParameter", ParameterValue = "some value" });

    // Add a project package (value) to fill a package parameter
    executionParameter.Add(new PackageInfo.ExecutionValueParameterSet { ObjectType = 30, ParameterName = "MyPackageParameter", ParameterValue = "some value" });

    // Get the identifier of the execution to get the log
    long executionIdentifier = ssisPackage.Execute(false, null, executionParameter);

    // Loop through the log and do something with it like adding to a list
    var messages = new List<string>();
    foreach (OperationMessage message in ssisServer.Catalogs["SSISDB"].Executions[executionIdentifier].Messages)
    {
        messages.Add(message.MessageType + ": " + message.Message);
    }

    return messages;
}

代码是http://social.technet.microsoft.com/wiki/contents/articles/21978.execute-ssis-2012-package-with-parameters-via-net.aspx?CommentPosted=true#commentmessage的轻微改编

http://domwritescode.com/2014/05/15/project-deployment-model-changes/也有类似的文章

【讨论】:

【解决方案3】:

要添加到@Craig Schwarze 的答案,

以下是一些相关的 MSDN 链接:

Loading and Running a Local Package Programmatically:

Loading and Running a Remote Package Programmatically

从正在运行的包中捕获事件:

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();
    }
  }
}

【讨论】:

    【解决方案4】:

    所以还有另一种方法可以让你从任何语言中启动它。 我认为最好的方法是,您可以创建一个批处理文件,该文件将调用您的 .dtsx 包。

    接下来,您可以从任何语言调用批处理文件。与在 Windows 平台中一样,您可以从任何地方运行批处理文件,我认为这将是最通用的方法。没有代码依赖。

    下面是一个博客,了解更多细节。

    https://www.mssqltips.com/sqlservertutorial/218/command-line-tool-to-execute-ssis-packages/

    编码愉快.. :)

    谢谢, 绫

    【讨论】:

      【解决方案5】:

      如果你在 SSIS 中有一些变量,你可以使用这个函数。

          Package pkg;
      
          Microsoft.SqlServer.Dts.Runtime.Application app;
          DTSExecResult pkgResults;
          Variables vars;
      
          app = new Microsoft.SqlServer.Dts.Runtime.Application();
          pkg = app.LoadPackage(" Location of your SSIS package", null);
      
          vars = pkg.Variables;
      
          // your variables
          vars["somevariable1"].Value = "yourvariable1";
          vars["somevariable2"].Value = "yourvariable2";
      
          pkgResults = pkg.Execute(null, vars, null, null, null);
      
          if (pkgResults == DTSExecResult.Success)
          {
              Console.WriteLine("Package ran successfully");
          }
          else
          {
      
              Console.WriteLine("Package failed");
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-09
        • 2017-01-01
        • 2016-01-25
        • 1970-01-01
        • 2021-03-02
        相关资源
        最近更新 更多