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.
First you need to add the variables and/or parameters to the readonly and/or readwrite variables. Edit the Script Task and in the Script Pane you will find two textboxes: ReadOnlyVariables and ReadWriteVariables. They are for variables and parameters. Note: parameters are always readonly.
Open the package in SQL Server Data Tools, and then click the Parameters tab in the SSIS Designer. Click the Add Parameter button on the toolbar. Enter values for the Name, Data Type, Value, Sensitive, and Required properties in the list itself or in the Properties window.
Basically, you can't pass parameters to a package. A package is just a wrapper around a set of functions, which you can call after the package has been imported.
There are three types of parameters that can be used within an Execute SQL Task in SSIS: Input parameters: used to pass a value as a parameter within a SQL command or stored procedure. Output parameters: used to store a value generated from an SQL command or stored procedure.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With