简体   繁体   English

如何使用从环境变量配置中设置的变量在SSIS包中设置变量?

[英]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. 我试图用C#编写一个程序,该程序将生成SSIS包。 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. 因此,例如,存在一个名为“ RootFolder”的变量,该变量是使用环境变量设置的,以为程序包建立一个工作文件夹。 There is another variable in the package named "OutputFolder", which takes the value of the RootFolder variable and appends the string "\\Output". 程序包中还有一个名为“ OutputFolder”的变量,该变量采用RootFolder变量的值并附加字符串“ \\ Output”。 Pretty straight forward, the original developer used BIDS Helper to set the following expression for OutputFolder: @[Template::RootFolder] + "\\Output\\" 很简单,原始开发人员使用BIDS Helper为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. 我在C#中构建此文件时遇到了一个问题,因为似乎在创建变量之前未设置环境变量,因此子变量似乎仅具有附加数据。 Using the example above, my "OutputFolder" variable is equal to "\\OutPut\\". 使用上面的示例,我的“ OutputFolder”变量等于“ \\ 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. 这是我用来设置配置以指向环境变量并从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. 使用该程序创建包后,RootFolder变量将调用环境变量并已正确设置,并且在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. 现在暂时忽略配置步骤,您应该在模板命名空间中的包中添加一个名为OutputFolder的变量。 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\\ 问题是您正在尝试将此变量的值分配为“ FolderRoot的值与\\ Output \\串联”,这将在执行代码时得到评估,从而导致您看到的是\\ Output \\

Instead, what you want to do is have the value of the variable OutputFolder be the Expression that you are assigning. 相反,您要执行的操作是将变量OutputFolder的值设置为要分配的表达式 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. 要使用BIDS做到这一点,您需要将该表达式分配给Expression属性, 并将 EvaluateAsExpression翻转为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 我的博客文章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: 使用billinkc提供的方法和链接,我可以使BIDS使用以下代码成功生成变量值:

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. 请将您的答案积分应用于Billinkc的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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