以编程方式在自定义SSIS组件上设置属性表达式

时间:2015-01-20 02:15:36

标签: c# ssis

我为我的SSIS包创建了一个自定义组件,并希望它尝试自动链接一些变量。目前我通过UI Properties-> Misc-> Expressions->(选择属性名称和表达式链接到它)手动链接它们,如下所示:

enter image description here

我目前正在努力使用以下代码:

public override void InitializeTask(Connections connections, VariableDispenser variableDispenser, IDTSInfoEvents events, IDTSLogging log, EventInfos eventInfos, LogEntryInfos logEntryInfos, ObjectReferenceTracker refTracker)
{
    base.InitializeTask(connections, variableDispenser, events, log, eventInfos, logEntryInfos, refTracker);

    using (Package pkg = ... /*not sure how to get 'this' package*/)
    {
        // Iterate through executables to try to find this TaskHost.
        foreach (Executable pExec in pkg.Executables)
        {
            TaskHost aTaskHost = (TaskHost)pExec;
            if (aTaskHost.InnerObject.GetType() == this.GetType())
            {
                // Iterate over variables, to try to match some task's properties.
                foreach (Variable myVar in pkg.Variables)
                {
                    if (aTaskHost.Properties.Contains("PackageName") &&
                        Regex.IsMatch(myVar.QualifiedName, "PackageName", RegexOptions.IgnoreCase))
                    {
                        aTaskHost.SetExpression("PackageName", myVar.Expression);
                    }
                }
                break;
            }
        }
    }
}

我试图一直向下设置表达水平;我的一些其他变量可能会改变,而不是链接.Value,我想转到.Expression。

但是,我不知道如何获得当前的包,或者即使我得到它,一切都将正确链接。所以,我想知道是否有人在初始化时以编程方式将系统变量链接到自定义组件的属性表达式。

1 个答案:

答案 0 :(得分:0)

我看到,通过UI Properties完成此操作是没有办法的。但是,如果通过SSIS UI连接,那么它可以工作。下面的片段是我采用的方法。希望它可以帮助其他想要更多地使用自定义组件的人。

因此,如果您有以下SSIS UI类:

class PackageStatusSSISUI : Microsoft.SqlServer.Dts.Runtime.Design.IDtsTaskUI
{
    private TaskHost _taskHost;
    private IServiceProvider _serviceProvider;

    public System.Windows.Forms.ContainerControl GetView()
    {
        PackageStatusForm editor = new PackageStatusForm(this._taskHost, this._serviceProvider);
        return editor;
    }

    public void Initialize(TaskHost taskHost, IServiceProvider serviceProvider)
    {
        this._taskHost = taskHost;
        this._serviceProvider = serviceProvider;
    }
}

然后,如果窗口形成连接到它,您可以访问任务主机变量并设置表达式:

public partial class PackageStatusForm : Form
{
    public PackageStatusForm(TaskHost taskHost, IServiceProvider serviceprovider)
    {
        this.TaskHost = taskHost;
        this.ServiceProvider = serviceprovider;

        InitializeComponent();

        if (this.TaskHost != null && this.TaskHost.Variables != null)
        {
            // Goes though each variable, to display the shortcut
            foreach (Variable var in taskHost.Variables)
            {
                variableList.Items.Add(var.QualifiedName);
            }

            // Can set labels to variable name, text boxes to allow the expressions, etc
            foreach (LabelTextDisplay ppt in this.flowLayoutPanel1.Controls)
            {
                this.TaskHost.SetExpression(ppt.PropertyName, ppt.ExpressionValue);
            }
        }
    }
}