简体   繁体   English

从SSIS 2012中的脚本组件中的PipelineBuffer获取列名

[英]Getting column name from PipelineBuffer in Script Component in SSIS 2012

I'm trying to get the column name and index from the PipelineBuffer in my script component transformation is SSIS and add them to a Hashtable. 我试图在我的脚本组件转换为SSIS中从PipelineBuffer获取列名和索引,然后将它们添加到Hashtable中。 I know this is possible if I change my class from: public class ScriptMain : UserComponent to ScriptMain : PipelineComponent and use this code: 我知道如果将我的类从: 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);
    }
}

However; 然而; when I do this I can no longer override: public override void Input0_ProcessInputRow(Input0Buffer Row) Since this is not available in the PipelineComponent class and I can no longer access my connection managers by simply calling something like this: IDTSConnectionManager100 connMgr = this.Connections.DbConnection; 当我这样做时,我将无法再覆盖: public override void Input0_ProcessInputRow(Input0Buffer Row)由于在PipelineComponent类中不可用,并且我无法再通过简单地调用以下代码来访问我的连接管理器: IDTSConnectionManager100 connMgr = this.Connections.DbConnection; From what I can see the BufferManager is not available in the UserComponent class. 从我可以看到的BufferManager在UserComponent类中不可用。 Is there a way to accomplish this using the UserComponent? 有没有一种方法可以使用UserComponent完成此操作?

Buddy of mine worked through this with me. 我的伙伴与我一起解决了这个问题。 You can get the name of the column coming in the script buffer like this: 您可以这样获取脚本缓冲区中的列名称:

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

You can get the column index and name in the script buffer by using reflection in the script component and loading them into a filtered list like this: 您可以通过在脚本组件中使用反射并将它们加载到过滤列表中来获得脚本缓冲区中的列索引和名称,如下所示:

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);
                    }

You can then access the list to get the index value in the script buffer using the name of the PropertyInfo object: 然后,您可以使用PropertyInfo对象的名称访问列表以获取脚本缓冲区中的索引值:

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

In order to then link this up with the index of the column in the input pipeline buffer you need to create a class attribute: 为了将其与输入管道缓冲区中列的索引链接起来,您需要创建一个class属性:

int[] BufferColumnIndexes; 

Then override ProcessInput and add the indexes from the input pipeline buffer that map to the script buffer indexes: 然后重写ProcessInput并从映射到脚本缓冲区索引的输入管道缓冲区中添加索引:

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

Now to link these up: 现在将它们链接起来:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM