Pass SSIS parameters from C#

2019-03-20 05:15发布

问题:

I have an SSIS package which has some Project.params set.

How do I pass values for those parameters to the SSIS package via C#?

I'm trying the following:

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

var results = pkg.Execute();

This returns a failure, with Errors collection containing "The variable "$Project::connString" was not found in the Variables collection. The variable might not exist in the correct scope."

So I tried adding

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

But this throws a DtsGenericException.

回答1:

I think I got it. The trick is to deserialize your ispac file (VS builds this but you can do it via msbuild) into a Project object. The Project object allows us to set project level parameters (as well as access project level connection managers).

From there we will need to get a reference to the specific package we want but it will be a PackageItem. PackageItems can't run but they do have a Package property we will use to instantiate the Package class which does have an Execute method

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

This assumes you have an SSIS project called so_31812951 which compiled to an ispac located at C:\sandbox\so_31812951\so_31812951\bin\Development\so_31812951.ispac This project has a single package called Package.dtsx. There will be a Project level parameter, Int32, named ProjectParameter as well as a package level parameter, Int32, named PackageParam



回答2:

I believe you will need to utilize the ParameterValue class.

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

The report service will have a method:

SetExecutionParmeters(parameters, language).

I am assuming you are loading the report, "export.dtsx", into SSIS and executing via the report service.



标签: c# ssis