【问题标题】:Getting column name from PipelineBuffer in Script Component in SSIS 2012从 SSIS 2012 脚本组件中的 PipelineBuffer 获取列名
【发布时间】:2014-03-30 17:06:29
【问题描述】:

我试图从我的脚本组件转换为 SSIS 的 PipelineBuffer 中获取列名和索引,并将它们添加到哈希表中。我知道如果我将课程从:public class ScriptMain : UserComponent 更改为 ScriptMain : PipelineComponent 并使用此代码,这是可能的:

public override void ProcessInput(int InputID, Microsoft.SqlServer.Dts.Pipeline.PipelineBuffer Buffer)
{
    inputBuffer = Buffer;
    hash = new Hashtable();
    IDTSInput100 i = ComponentMetaData.InputCollection.GetObjectByID(InputID);
    foreach (IDTSInputColumn100 col in i.InputColumnCollection)
    {
        int colIndex = BufferManager.FindColumnByLineageID(i.Buffer, col.LineageID);
        hash.Add(col.Name, colIndex);
    }
}

但是;当我这样做时,我不能再覆盖:public override void Input0_ProcessInputRow(Input0Buffer Row) 因为这在 PipelineComponent 类中不可用,我不能再通过简单地调用这样的东西来访问我的连接管理器:IDTSConnectionManager100 connMgr = this.Connections.DbConnection; 从我可以看到 BufferManager 不是在 UserComponent 类中可用。有没有办法使用 UserComponent 来实现这一点?

【问题讨论】:

    标签: c# ssis script-component


    【解决方案1】:

    我的朋友和我一起解决了这个问题。您可以像这样获取脚本缓冲区中列的名称:

    public override void Input0_ProcessInputRow(Input0Buffer inputBufferRow)
         {
        foreach (IDTSInputColumn100 column in this.ComponentMetaData.InputCollection[0].InputColumnCollection)
                { 
                  PropertyInfo columnValue = inputBufferRow.GetType().GetProperty(column.Name);
                }
           }
    

    您可以通过在脚本组件中使用反射并将它们加载到过滤列表中来获取脚本缓冲区中的列索引和名称,如下所示:

    IList<string> propertyList = new List<string>();
                        var properties = typeof(Input0Buffer).GetProperties();
                        foreach (var property in properties)
                        {
                            if (!property.Name.EndsWith("_IsNull"))
                                propertyList.Add(property.Name);
                        }
    

    然后您可以使用 PropertyInfo 对象的名称访问该列表以获取脚本缓冲区中的索引值:

    int index = (propertyList.IndexOf(columnValue.Name));
    

    为了将它与输入管道缓冲区中列的索引链接起来,您需要创建一个类属性:

    int[] BufferColumnIndexes; 
    

    然后覆盖 ProcessInput 并添加从输入管道缓冲区映射到脚本缓冲区索引的索引:

    public override void ProcessInput(int InputID, Microsoft.SqlServer.Dts.Pipeline.PipelineBuffer Buffer)
        {
            inputBuffer = Buffer;
            BufferColumnIndexes = GetColumnIndexes(InputID);
            base.ProcessInput(InputID, Buffer);
        }
    

    现在将这些链接起来:

    int index = (propertyList.IndexOf(columnValue.Name)); //index in script buffer
    int index2 = (BufferColumnIndexes[index]); //index in input pipeline buffer
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多