C# 如何访问脚本组件内的 ssis 包变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13450289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:35:44  来源:igfitidea点击:

How to access ssis package variables inside script component

c#sqldatabasessisbids

提问by Ashfaq Ahmed

How can I access variables inside my C# code which I've used in Data Flow -> Script Component - > My c# Script with my SSIS package?

如何访问我在数据流 -> 脚本组件 -> 带有 SSIS 包的 C# 脚本中使用的 C# 代码中的变量?

I have tried with which is also not working

我试过它也不起作用

IDTSVariables100 varCollection = null;
this.VariableDispenser.LockForRead("User::FilePath");
string XlsFile;

XlsFile = varCollection["User::FilePath"].Value.ToString();

采纳答案by Edmund Schweppe

Accessing package variables in a Script Component(of a Data Flow Task) is not the same as accessing package variables in a Script Task. For a Script Component, you first need to open the Script Transformation Editor(right-click on the component and select "Edit..."). In the Custom Properties section of the Script tab, you can enter (or select) the properties you want to make available to the script, either on a read-only or read-write basis: screenshot of Script Transformation Editor properties pageThen, within the script itself, the variables will be available as strongly-typed properties of the Variables object:

在(数据流任务的)脚本组件中访问包变量与在脚本任务中访问包变量不同。对于脚本组件,您首先需要打开脚本转换编辑器(右键单击该组件并选择“编辑...”)。在“脚本”选项卡的“自定义属性”部分中,您可以输入(或选择)要在只读或读写基础上提供给脚本的属性: 脚本转换编辑器属性页面的屏幕截图然后,在脚本本身中,变量将可用作 Variables 对象的强类型属性:

// Modify as necessary
public override void PreExecute()
{
    base.PreExecute();
    string thePath = Variables.FilePath;
    // Do something ...
}

public override void PostExecute()
{
    base.PostExecute();
    string theNewValue = "";
    // Do something to figure out the new value...
    Variables.FilePath = theNewValue;
}

public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    string thePath = Variables.FilePath;
    // Do whatever needs doing here ...
}

One important caveat: if you need to writeto a package variable, you can only do so in the PostExecute() method.

一个重要的警告:如果您需要写入包变量,则只能在 PostExecute() 方法中执行此操作。

Regarding the code snippet:

关于代码片段:

IDTSVariables100 varCollection = null;
this.VariableDispenser.LockForRead("User::FilePath");
string XlsFile;

XlsFile = varCollection["User::FilePath"].Value.ToString();

varCollectionis initialized to null and never set to a valid value. Thus, anyattempt to dereference it will fail.

varCollection初始化为 null 并且从不设置为有效值。因此,任何取消引用它的尝试都将失败。

回答by Daryl Wenman-Bateson

  • On the front properties page of the variable script, amend the ReadOnlyVariables (or ReadWriteVariables) property and select the variables you are interested in. This will enable the selected variables within the script task
  • Within code you will now have access to read the variable as

    string myString = Variables.MyVariableName.ToString();

  • 在变量脚本的前端属性页面上,修改 ReadOnlyVariables(或 ReadWriteVariables)属性并选择您感兴趣的变量。这将启用脚本任务中的选定变量
  • 在代码中,您现在可以将变量读取为

    string myString = Variables.MyVariableName.ToString();

回答by user2519563

Strongly typed var don't seem to be available, I have to do the following in order to get access to them:

强类型 var 似乎不可用,我必须执行以下操作才能访问它们:

String MyVar = Dts.Variables["MyVarName"].Value.ToString();

String MyVar = Dts.Variables["MyVarName"].Value.ToString();

回答by prot

This should work:

这应该有效:

IDTSVariables100 vars = null;
VariableDispenser.LockForRead("System::TaskName");
VariableDispenser.GetVariables(vars);
string TaskName = vars("System::TaskName").Value.ToString();
vars.Unlock();

Your initial code lacks call of the GetVariables() method.

您的初始代码缺少对 GetVariables() 方法的调用。

回答by Marco Rosas

I had the same problem as the OP except I remembered to declare the ReadOnlyVariables.

除了我记得声明 ReadOnlyVariables 之外,我遇到了与 OP 相同的问题。

After some playing around, I discovered it was the name of my variable that was the issue. "File_Path" in SSIS somehow got converted to "FilePath". C# does not play nicely with underscores in variable names.

经过一番玩耍后,我发现问题在于我的变量的名称。SSIS 中的“File_Path”不知何故被转换为“FilePath”。C# 不能很好地与变量名称中的下划线配合使用。

So to access the variable, I type

所以要访问变量,我输入

string fp = Variables.FilePath;

In the PreExecute() method of the Script Component.

在脚本组件的 PreExecute() 方法中。

回答by Adithya Alapati

First List the Variable that you want to use them in Script task at ReadOnlyVariables in the Script task editor and Edit the Script

首先在脚本任务编辑器的 ReadOnlyVariables 处的脚本任务中列出要使用的变量,然后编辑脚本

To use your ReadOnlyVariables in script code

在脚本代码中使用 ReadOnlyVariables

String codeVariable = Dts.Variables["User::VariableNameinSSIS"].Value.ToString();

this line of code will treat the ssis package variable as a string.

这行代码会将 ssis 包变量视为字符串。