如何在脚本组件中访问ssis包变量

Ash*_*med 38 c# sql database ssis bids

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

我试过用哪个也行不通

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

XlsFile = varCollection["User::FilePath"].Value.ToString();
Run Code Online (Sandbox Code Playgroud)

Edm*_*ppe 60

在脚本组件(数据流任务)中访问包变量与在脚本任务中访问包变量不同.对于脚本组件,首先需要打开脚本转换编辑器(右键单击组件并选择"编辑...").在"脚本"选项卡的"自定义属性"部分中,您可以以只读或读写方式输入(或选择)要使脚本可用的属性: 脚本转换编辑器属性页面的屏幕截图 然后,在脚本本身中,变量将作为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 ...
}
Run Code Online (Sandbox Code Playgroud)

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

关于代码片段:

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

XlsFile = varCollection["User::FilePath"].Value.ToString();
Run Code Online (Sandbox Code Playgroud)

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


小智 6

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

在脚本代码中使用ReadOnlyVariables

String codeVariable = Dts.Variables["User::VariableNameinSSIS"].Value.ToString();
Run Code Online (Sandbox Code Playgroud)

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