当前位置:  开发笔记 > 编程语言 > 正文

如何从.NET执行SSIS包?

如何解决《如何从.NET执行SSIS包?》经验,为你挑选了3个好方法。

我有一个SSIS包,最终我也想传递参数,这些参数将来自.NET应用程序(VB或C#),所以我很好奇,如果有人知道如何做到这一点,或者更好的是一个有帮助提示的网站如何做到这一点.所以基本上我想从.NET执行一个SSIS包传递它可以在其中使用的SSIS包参数.例如,SSIS包将使用平面文件导入到SQL数据库中,但文件的路径和名称可以是从.Net应用程序传递的参数.



1> Craig Schwar..:

以下是如何从代码中设置包中的变量 -

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


好的,谢谢@Spikeh!值得注意的是,当我最近实现类似的代码来加载带有Dts的SSIS包时,我不得不在`C:\ Windows\assembly`文件夹中从"GAC"手动获取`Microsoft.SqlServer.ManagedDTS.dll`文件. ,编译这样的代码.
@IanCampbell是的,DTS是折旧的(事实上,我认为你甚至不能将DTS与最新版本的SQL Server一起使用 - 而不是我试图找到它!).但是,包含某些SSIS组件的.Net命名空间仍包含Dts字.我保证这是当前版本并且有效.
是的,我也一样 - 我昨天做的一样!我正在使用VS2012和.Net 4(用于SSIS包)/ 4.5(用于我的单元测试).我必须从C:\ Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SqlServer.ManagedDTS\v4.0_11.0.0.0__89845dcd8080cc91获取程序集,因为它在任何其他程序集文件夹中都不存在,或者在SQL文件夹.
@IanCampbell我假设您指的是Microsoft.SqlServer.Dts.Runtime?Dts只是SSIS的遗留名称 - 它只是名称空间声明.上面的代码将继续得到支持.

2> Paul Hatcher..:

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

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

using Microsoft.SqlServer.Management.IntegrationServices;

public List 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 executionParameter = new Collection();

    // 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();
    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/上也有类似的文章


显然它只在GAC中:[Microsoft.SqlServer.Management.IntegrationServices.dll程序集位置](http://muxtonmumbles.blogspot.com.au/2012/08/programmatically-executing-packages-in.html)

3> Faiz..:

要添加@Craig Schwarze答案,

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

以编程方式加载和运行本地程序包:

以编程方式加载和运行远程包

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

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

推荐阅读
落单鸟人
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有