簡體   English   中英

從SSIS 2012中的腳本組件中的PipelineBuffer獲取列名

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

我試圖在我的腳本組件轉換為SSIS中從PipelineBuffer獲取列名和索引,然后將它們添加到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);
    }
}

然而; 當我這樣做時,我將無法再覆蓋: public override void Input0_ProcessInputRow(Input0Buffer Row)由於在PipelineComponent類中不可用,並且我無法再通過簡單地調用以下代碼來訪問我的連接管理器: IDTSConnectionManager100 connMgr = this.Connections.DbConnection; 從我可以看到的BufferManager在UserComponent類中不可用。 有沒有一種方法可以使用UserComponent完成此操作?

我的伙伴與我一起解決了這個問題。 您可以這樣獲取腳本緩沖區中的列名稱:

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

為了將其與輸入管道緩沖區中列的索引鏈接起來,您需要創建一個class屬性:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM