简体   繁体   中英

How do I set variables in an SSIS package using variables that are set from a Environment Variable Configuration?

Apologies for the rather lengthy title, but that seemed to be the briefest way to describe the problem I was running into.

I'm trying to write a program in C# that will generate SSIS packages. I do have existing packages that I could use as a template and in the existing packages, there are variables that use other variables which are populated using environment variables. So for example, there is a variable named "RootFolder", which is set using an environment variable to establish a working folder for the package. There is another variable in the package named "OutputFolder", which takes the value of the RootFolder variable and appends the string "\\Output". Pretty straight forward, the original developer used BIDS Helper to set the following expression for OutputFolder: @[Template::RootFolder] + "\\Output\\"

I have run into a bit of an issue building this in C#, as it appears that the environment variable is not being set before the variables are created, so the child variables only appear to have the appended data. Using the example above, my "OutputFolder" variable is equal to "\\OutPut\\".

Here's a snippet of the code I'm using to set the configuration to point to an environment variable, and stacking variables off RootFolder.

pkg.Variables.Add("FolderRoot", false, "Template", "");

Configuration configRootFolder = pkg.Configurations.Add();
configRootFolder.Name = @"RootFolder";
configRootFolder.ConfigurationType = Microsoft.SqlServer.Dts.Runtime.DTSConfigurationType.EnvVariable;
configRootFolder.ConfigurationString = @"Ssis_RootFolder";
configRootFolder.PackagePath = "\\Package.Variables[Template::FolderRoot].Properties[Value]";

pkg.Variables.Add("OutputFolder", false, "Template", pkg.Variables["Template::FolderRoot"].Value + "\\Output\\");

Any assistance would be greatly appreciated. Thanks in advance! Rich

EDIT : Sorry, I realized I missed an important detail:

  • After the package has been created using the program, the RootFolder variable is calling the Environment Variable and being set properly and is visible in Visual Studio.

You may be confusing things here. You have code to add variables, then you're applying configuration and then you're attempting to add another variable. Ignoring the configuration steps for now, you should be adding a variable to your package called OutputFolder in the Template namespace. The problem is that you are attempting to assign the value of this variable to be "FolderRoot's value concatenated with \\Output\\" That will be evaluated when the code is executed which results in what you are seeing as \\Output\\

Instead, what you want to do is have the value of the variable OutputFolder be the Expression that you are assigning. Slightly confusing until you look at the properties.

在此处输入图片说明

To make this happen using BIDS, you'd need to assign that expression to the Expression property and flip EvaluateAsExpression to True. In code, that'd be

Variable v = null;
v = pkg.Variables.Add("OutputFolder", false, "Template", String.Empty);
v.EvaluateAsExpression = true;
v.Expression = pkg.Variables["Template::FolderRoot"].Value + "\\Output\\";

More coding examples on my blog post Variables and Expressions with SSIS EzAPI

Using the method and link provided by billinkc, I was able to have BIDS generate a variable value successfully using the following code:

Variable v = null;
v = pkg.Variables.Add("OutputFolder", false, "Template", String.Empty);
v.EvaluateAsExpression = true;
v.Expression = "@[Template::FolderRoot] + \"\\\\Output\\\\\"";

Please apply your answer credit to billinkc's answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM