从C#传递SSIS参数

时间:2015-08-04 15:10:36

标签: c# ssis

我有一个SSIS包,其中设置了一些Project.params。

如何通过C#将这些参数的值传递给SSIS包?

我正在尝试以下方法:

const string pkgLocation = @"export.dtsx";
var app = new Application();
var pkg = app.LoadPackage(pkgLocation, null);

var results = pkg.Execute();

返回失败,Errors集合包含“变量”$ Project :: connString“在变量集合中找不到。变量可能不存在于正确的范围内。”

所以我尝试添加

var param = pkg.Parameters.Add("connString", TypeCode.String);
param.Value = "test"; 
var results = pkg.Execute();

但是这会引发DtsGenericException。

2 个答案:

答案 0 :(得分:6)

我想我明白了。诀窍是将您的ispac文件反序列化(VS构建此文件,但您可以通过msbuild执行此操作)到Project对象。 Project对象允许我们设置项目级参数(以及访问项目级连接管理器)。

从那里我们需要获得我们想要的特定包的引用,但它将是PackageItem。 PackageItems无法运行,但它们确实有一个Package属性,我们将使用它来实例化 具有Package方法的Execute

   public static void final()
    {
        string isPacPath = @"C:\sandbox\so_31812951\so_31812951\bin\Development\so_31812951.ispac";
        string packageName = "Package.dtsx";
        Application app = new Application();
        Package pkg = null;

        // https://msdn.microsoft.com/en-us/library/ff930196(v=sql.110).aspx
        Project proj = null;
        PackageItem pi = null;
        DTSExecResult results;

        ///////////////////////////////////////////////////////////////////
        // Run an SSIS package that has a Project parameter
        ///////////////////////////////////////////////////////////////////
        proj = Project.OpenProject(isPacPath);

        // Yes, I can see the packages in there
        foreach (var item in proj.PackageItems)
        {
            Console.WriteLine(string.Format("Project {0} contains package {1}", proj.Name, item.StreamName));
        }

        //Also able to see the project level parameters
        foreach (Parameter item in proj.Parameters)
        {
            Console.WriteLine(string.Format("Project {0} contains parameter {1} type of {2} current value {3}", proj.Name, item.Name, item.DataType, item.Value));
        }

        // assign a value to my project level parameter
        proj.Parameters["ProjectParameter"].Value = 10;

        // Get the package from the project collection
        pi = proj.PackageItems[packageName];

        // Convert the package into a package object
        pkg = pi.Package;

        // This is how we specify a package parameter value
        pkg.Parameters["PackageParam"].Value = 777;

        results = pkg.Execute();

        Console.WriteLine(results);
    }

这假设您有一个名为so_31812951的SSIS项目,该项目编译为位于C:\ sandbox \ so_31812951 \ so_31812951 \ bin \ Development \ so_31812951.ispac的ispac。此项目有一个名为Package.dtsx的包。将有一个名为ProjectParameter的项目级别参数Int32以及名为PackageParam的包级别参数Int32

答案 1 :(得分:-1)

我相信您需要使用ParameterValue类。

var parameters = new ParameterValue[somelength];
parameters[index] = new ParameterValue()
{
     Name = "parameter name",
     Value = "parameter value"
}

报告服务将有一个方法:

SetExecutionParmeters(parameters, language).

我假设您正在将报告“export.dtsx”加载到SSIS中并通过报告服务执行。