以编程方式创建RecordsetDestination

时间:2016-12-23 22:26:16

标签: c# ssis

我正在尝试用C#构建一个SSIS包。

我遇到的问题是我无法解决如何创建RecordsetDestination并将其绑定到返回SqlCommand结果的OLEDB数据源

我可以找到大量绑定到OLE目标的示例,但我不知道如何绑定到Recordset。

到目前为止,我已经能够创建我的数据流任务创建Ole Source,添加一个Object Variable来存储记录集结果。但是......我不知道如何创建记录集目标本身然后绑定它来源和可变量。

1 个答案:

答案 0 :(得分:1)

您将创建一个与创建Oledb源以创建Record Set Destination组件非常相似的过程;有一些小的变化。

  1. 将相应IDTSComponentMetaData100对象的ComponentClassID设置为“{C457FD7E-CE98-4C4B-AEFE-F3AE0044F181}”或“DtsAdapter.RecordSetDestination”。假设recSetComponentMetadataObject是该对象,它将如下所示:

    recSetComponentMetadataObject.ComponentClassID =“{C457FD7E-CE98-4C4B-AEFE-F3AE0044F181}”;

  2. 将RecordSet目标组件上的“VariableName”属性设置为用户变量。为此,您将在相应的CManagedComponentWrapper / IDTSDesigntimeComponent100对象上调用SetComponentProperty。假设recSetDesignTimeObject是该对象,它将如下所示:

    recSetDesignTimeObject.SetComponentProperty(“VariableName”,“User :: MyRecSetVariableName”);

  3. 创建一个连接Source组件和Record Set Destination
  4. 的新IDTSPath100
  5. 在要设置目标的列中设置使用类型(即存储在用户变量中的列)。
  6. 您将需要用于创建OleDb源组件的相同程序集引用。

    //Record Set Destination Component Instantiation
            IDTSComponentMetaData100 recSetComponentMetadataObject = pipeline.ComponentMetaDataCollection.New();
            recSetComponentMetadataObject.ComponentClassID = "{C457FD7E-CE98-4C4B-AEFE-F3AE0044F181}";
            //recSetComponentMetadataObject.ComponentClassID = "DtsAdapter.RecordSetDestination";
            CManagedComponentWrapper recSetDesignTimeObject = recSetComponentMetadataObject.Instantiate();
            recSetDesignTimeObject.ProvideComponentProperties();
    
            // name, description etc if needed
            recSetComponentMetadataObject.Name = "HoHoHo";
    
            // Setting the User Variable
            recSetDesignTimeObject.SetComponentProperty("VariableName", "User::MyRecSetVariableName");
    
            // Connect to previous component;
            // Assuming pipeline is the MainPipe object from the parent Data Flow Task and srcComponent is the IDTSComponentMetaData100 object of previous oledb source component
            IDTSPath100 path = pipeline.PathCollection.New();
            path.AttachPathAndPropagateNotifications(
                srcComponent.OutputCollection[0],
                recSetComponentMetadataObject.InputCollection[0]);
    
            // Set usage type to the required columns
            IDTSInput100 recSetInput = recSetComponentMetadataObject.InputCollection[0];
            IDTSVirtualInput100 virtualInput = recSetInput.GetVirtualInput();
    
            // call SetUsageType for each column you need. Here I am assuming I need columns "col_a" and col_b
            IDTSInputColumn100 inputColA = recSetDesignTimeObject.SetUsageType(
                recSetInput.ID,
                virtualInput,
                virtualInput.VirtualInputColumnCollection["col_a"].LineageID,
                DTSUsageType.UT_READONLY
                );
    
            IDTSInputColumn100 inputColB = recSetDesignTimeObject.SetUsageType(
                recSetInput.ID,
                virtualInput,
                virtualInput.VirtualInputColumnCollection["col_b"].LineageID,
                DTSUsageType.UT_READONLY
                );