且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何访问脚本组件内部的SSIS包变量

更新时间:2023-02-07 13:31:51

访问包变量在脚本中的组件(数据流任务)是不一样的剧本工作。对于一个脚本组件,您首先需要打开脚本转换编辑器一>(在组件上单击鼠标右键并选择Edit ...)。在脚本选项卡中的自定​​义属性部分,您可以输入(或选择),你要提供给脚本的性能,无论是在只读或读写的基础:
如何访问脚本组件内部的SSIS包变量
的屏幕截图然后,脚本本身,变量将是可以作为强类型的属性变量对象:

  //根据需要修改
公共覆盖无效PreExecute()
{
base.PreExecute();
串thePath = Variables.FilePath;
//做某事
}

公共覆盖无效PostExecute()
{
base.PostExecute();
串theNewValue =;
//做一些事情弄清楚了新的价值...
Variables.FilePath = theNewValue;
}

公共覆盖无效Input0_ProcessInputRow(Input0Buffer行)
{
串thePath = Variables.FilePath;
//什么就做什么需要在这里做...
}



一个重要警告:如果您需要的,以包变量,你只能做这样的PostExecute()方法。



关于代码片段

  IDTSVa​​riables100 varCollection = NULL; 
this.VariableDispenser.LockForRead(用户::文件路径);
串XlsFile;

XlsFile = varCollection [用户::文件路径] Value.ToString();



varCollection 被初始化为null,从未设置一个有效的价值。因此,任何尝试取消对它的引用将会失败。


i am new in SSIS, working on a project, and trying to access SSIS package variables inside my c# code which i have used in Data Flow -> Script Component - > My c# Script

can anyone help on this ?

also 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();

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: Then, within the script itself, the variables will be available as strongly-typed properties of the Variables object:

// 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 write to a package variable, you can only do so in the PostExecute() method.

Regarding the code snippet:

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

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

varCollection is initialized to null and never set to a valid value. Thus, any attempt to dereference it will fail.