从C#传递SSIS参数

Mat*_*ear 7 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();
Run Code Online (Sandbox Code Playgroud)

这将返回失败,并且在Variables集合中找不到包含"变量"$ Project :: connString"的Errors集合.变量可能不存在于正确的范围内."

所以我尝试添加

var param = pkg.Parameters.Add("connString", TypeCode.String);
param.Value = "test"; 
var results = pkg.Execute();
Run Code Online (Sandbox Code Playgroud)

但是这会引发DtsGenericException.

bil*_*nkc 6

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

从那里我们需要获得我们想要的特定包的引用,但它将是一个PackageItem.PackageItems不能运行,但他们有一个包属性,我们将用它来实例化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);
    }
Run Code Online (Sandbox Code Playgroud)

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


小智 -1

我相信您将需要利用 ParameterValue 类。

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

报告服务将有一个方法:

SetExecutionParmeters(parameters, language).
Run Code Online (Sandbox Code Playgroud)

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