c# - 如何在脚本组件中访问 ssis 包变量

标签 c# sql database ssis bids

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

我试过还是不行

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

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

最佳答案

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

关于代码片段:

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

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

varCollection 初始化为 null 并且从未设置为有效值。因此,任何取消引用它的尝试都将失败。

关于c# - 如何在脚本组件中访问 ssis 包变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13450289/

相关文章:

database - 按降序索引日期列是个好主意吗?

c# - 如何在c#的main方法中调用我的方法?

c# - 单击按钮后刷新 Gridview

mysql - SQL计数函数equals运算符和in运算符之间的区别

database - 在elasticsearch中存储搜索数据的推荐方式是什么

database - 需要字典数据库

c# - 我如何启动这样的自定义变量; "Serie S = new Serie() { 1, 2, 3, 4 }"?

c# - 如何将颜色转换为代码,反之亦然

c# - EF Core 添加迁移调试

sql - 何时为两种略有不同的信息使用单独的 SQL 数据库表?