【问题标题】:How to access ssis package variables inside script component如何在脚本组件中访问 ssis 包变量
【发布时间】:2012-11-07 04:01:22
【问题描述】:

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

我试过了,哪个也不行

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

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

【问题讨论】:

  • 您遇到了什么错误或意外行为?

标签: c# sql database ssis bids


【解决方案1】:
  • 在变量脚本的前端属性页面,修改 ReadOnlyVariables(或 ReadWriteVariables)属性并选择您感兴趣的变量。这将启用脚本任务中的选定变量
  • 在代码中,您现在可以将变量读取为

    string myString = Variables.MyVariableName.ToString();

【讨论】:

  • 这仍然正确吗?我似乎无权访问这些变量
【解决方案2】:

访问脚本组件(数据流任务)中的包变量与访问脚本任务中的包变量不同。对于脚本组件,您首先需要打开Script Transformation Editor(右键单击该组件并选择“编辑...”)。在脚本选项卡的自定义属性部分,您可以输入(或选择)您希望脚本可用的属性,无论是只读还是读写: 然后,在脚本本身中,变量将作为 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 ...
}

一个重要的警告:如果您需要写入到包变量,您只能在 PostExecute() 方法中这样做。

关于代码sn-p:

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

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

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

【讨论】:

    【解决方案3】:

    强类型的 var 似乎不可用,我必须执行以下操作才能访问它们:

    String MyVar = Dts.Variables["MyVarName"].Value.ToString();

    【讨论】:

    • 这个答案似乎与脚本任务有关(在控制流中),而问题与脚本组件(在数据流中)有关。
    【解决方案4】:

    这应该可行:

    IDTSVariables100 vars = null;
    VariableDispenser.LockForRead("System::TaskName");
    VariableDispenser.GetVariables(vars);
    string TaskName = vars["System::TaskName"].Value.ToString();
    vars.Unlock();
    

    您的初始代码缺少对 GetVariables() 方法的调用。

    【讨论】:

      【解决方案5】:

      除了我记得声明 ReadOnlyVariables 之外,我遇到了与 OP 相同的问题。

      玩了一些之后,我发现问题出在我的变量名上。 SSIS 中的“File_Path”不知何故被转换为“FilePath”。 C# 不能很好地处理变量名中的下划线。

      所以要访问变量,我输入

      string fp = Variables.FilePath;
      

      在脚本组件的 PreExecute() 方法中。

      【讨论】:

        【解决方案6】:

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

        在脚本代码中使用您的 ReadOnlyVariables

        String codeVariable = Dts.Variables["User::VariableNameinSSIS"].Value.ToString();
        

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-05
          • 2018-01-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-18
          • 1970-01-01
          相关资源
          最近更新 更多