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

jym*_*mbo 1 c# ssis script-component

我试图在我的脚本组件转换中从PipelineBuffer获取列名和索引是SSIS并将它们添加到Hashtable.我知道如果我将我的类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);
    }
}
Run Code Online (Sandbox Code Playgroud)

然而; 当我这样做时,我不能再覆盖:public override void Input0_ProcessInputRow(Input0Buffer Row)因为这在PipelineComponent类中不可用,并且我不能再通过调用这样的东西来访问我的连接管理器:IDTSConnectionManager100 connMgr = this.Connections.DbConnection;从我所看到的,UserComponent类中没有BufferManager.有没有办法使用UserComponent完成此任务?

jym*_*mbo 6

我的好友和我一起工作.您可以像下面这样获取脚本缓冲区中列的名称:

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

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

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);
                    }
Run Code Online (Sandbox Code Playgroud)

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

int index = (propertyList.IndexOf(columnValue.Name));
Run Code Online (Sandbox Code Playgroud)

然后,为了将其与输入管道缓冲区中的列索引相关联,您需要创建一个类属性:

int[] BufferColumnIndexes; 
Run Code Online (Sandbox Code Playgroud)

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

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

现在将这些链接起来:

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