Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何访问脚本组件内的ssis包变量_C#_Sql_Database_Ssis_Bids - Fatal编程技术网

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

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

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

我试过,但也不起作用

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包变量视为字符串。

您遇到的错误或意外行为是什么?此答案似乎与脚本任务(在控制流中)有关,而问题与脚本组件(在数据流中)有关。这是否仍然正确?我似乎无法访问这些变量