C# 如何访问脚本组件内的ssis包变量
如何访问我在数据流->脚本组件->带有SSIS包的C#脚本中使用的C#代码中的变量 我试过,但也不起作用C# 如何访问脚本组件内的ssis包变量,c#,sql,database,ssis,bids,C#,Sql,Database,Ssis,Bids,如何访问我在数据流->脚本组件->带有SSIS包的C#脚本中使用的C#代码中的变量 我试过,但也不起作用 IDTSVariables100 varCollection = null; this.VariableDispenser.LockForRead("User::FilePath"); string XlsFile; XlsFile = varCollection["User::FilePath"].Value.ToString(); 在变量脚本的首页属性页上,修改ReadOnlyVa
IDTSVariables100 varCollection = null;
this.VariableDispenser.LockForRead("User::FilePath");
string XlsFile;
XlsFile = varCollection["User::FilePath"].Value.ToString();
- 在变量脚本的首页属性页上,修改ReadOnlyVariables(或ReadWriteVariables)属性并选择您感兴趣的变量。这将在脚本任务中启用选定的变量
- 在代码中,您现在可以将变量读取为 字符串myString=Variables.MyVariableName.ToString()
// 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 ...
}
一个重要的警告:如果需要写入包变量,则只能在PostExecute()方法中执行
关于代码片段:
IDTSVariables100 varCollection = null;
this.VariableDispenser.LockForRead("User::FilePath");
string XlsFile;
XlsFile = varCollection["User::FilePath"].Value.ToString();
varCollection
初始化为null,并且从未设置为有效值。因此,任何尝试取消引用它的操作都将失败。强类型var似乎不可用,我必须执行以下操作才能访问它们:
String MyVar=Dts.Variables[“MyVarName”].Value.ToString()代码>这应该可以:
IDTSVariables100 vars = null;
VariableDispenser.LockForRead("System::TaskName");
VariableDispenser.GetVariables(vars);
string TaskName = vars["System::TaskName"].Value.ToString();
vars.Unlock();
您的初始代码缺少对GetVariables()方法的调用。除了我记得声明ReadOnlyVariables之外,我遇到了与OP相同的问题
经过一番尝试,我发现问题出在变量的名称上。SSIS中的“文件路径”以某种方式被转换为“文件路径”。C#不能很好地处理变量名中的下划线
因此,要访问变量,我键入
string fp = Variables.FilePath;
在脚本组件的PreExecute()方法中。首先在脚本任务编辑器的ReadOnlyVariables中列出要在脚本任务中使用的变量,然后编辑脚本
在脚本代码中使用ReadOnlyVariable
String codeVariable = Dts.Variables["User::VariableNameinSSIS"].Value.ToString();
这行代码将ssis包变量视为字符串。您遇到的错误或意外行为是什么?此答案似乎与脚本任务(在控制流中)有关,而问题与脚本组件(在数据流中)有关。这是否仍然正确?我似乎无法访问这些变量