访问(数据流任务的)脚本Component中的包变量与访问脚本Task中的包变量不同.对于脚本组件,首先需要打开Script Transformation Editor(右键单击组件并 Select "编辑…").在"脚本"选项卡的"自定义属性"部分中,可以输入(或 Select )要使脚本可用的属性(只读或读写):
// 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 ...
}
一个重要的警告:如果需要将包变量设置为write,则只能在PostExecute()方法中执行此操作.
关于代码片段:
IDTSVariables100 varCollection = null;
this.VariableDispenser.LockForRead("User::FilePath");
string XlsFile;
XlsFile = varCollection["User::FilePath"].Value.ToString();
varCollection
初始化为null,并且从未设置为有效值.因此,try 取消引用它将失败.