以程式設計方式加入資料流程元件

當您建立資料流程時,可以從加入元件開始。接著您會設定這些元件,然後將它們連接在一起以便在執行階段建立資料流程。本章節描述將元件加入資料流程工作,建立元件的設計階段執行個體,然後設定元件。如需有關如何連接元件的詳細資訊,請參閱<以程式設計的方式連接資料流程元件>。

加入元件

呼叫 ComponentMetaDataCollection 集合的 New 方法,以建立新元件並將它加入資料流程工作中。這個方法會傳回元件的 IDTSComponentMetaData100 介面。不過,此時,IDTSComponentMetaData100 並不包含任何一個元件的特定資訊。設定 ComponentClassID 屬性以識別元件的類型。資料流程工作使用此屬性值在執行階段建立元件的執行個體。

ComponentClassID 屬性中指定的值,可以是 CLSID、PROGID 或是元件的 CreationName 屬性。CLSID 通常會顯示在 [屬性] 視窗中,做為元件的 ComponentClassID 屬性值。如需有關取得此屬性以及可用元件之其他屬性的詳細資訊,請參閱<以程式設計方式探索資料流程元件>。

加入 Managed 元件

您無法使用 CLSID 或 PROGID 將 Managed 資料流程元件加入資料流程,因為這些值會指向包裝函式,而不是元件本身。您可以改用 CreationName 屬性或是 AssemblyQualifiedName 屬性,如下列範例所示。

如果您打算使用 AssemblyQualifiedName 屬性,則必須在 Visual Studio 專案中加入含有 Managed 元件之組件的參考。這些組件不會列在 [加入參考] 對話方塊的 [.NET] 索引標籤上。一般來說,您必須經由瀏覽,才可從 C:\Program Files\Microsoft SQL Server\100\DTS\PipelineComponents 資料夾中找到該項組件。

內建的 Managed 資料流程元件包括:

  • ADO.NET 來源

  • XML 來源

  • DataReader 目的地

  • SQL Server Compact 目的地

  • 指令碼元件

下列程式碼範例顯示兩種將 Managed 元件加入資料流程的方法:

using System;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;

namespace Microsoft.SqlServer.Dts.Samples
{
  class Program
  {
    static void Main(string[] args)
    {
      Microsoft.SqlServer.Dts.Runtime.Package package = new Microsoft.SqlServer.Dts.Runtime.Package();
      Executable e = package.Executables.Add("STOCK:PipelineTask");
      Microsoft.SqlServer.Dts.Runtime.TaskHost thMainPipe = (Microsoft.SqlServer.Dts.Runtime.TaskHost)e;
      MainPipe dataFlowTask = (MainPipe)thMainPipe.InnerObject;

      // The Application object will be used to obtain the CreationName
      //  of a PipelineComponentInfo from its PipelineComponentInfos collection.
      Application app = new Application();

      // Add a first ADO NET source to the data flow.
      //  The CreationName property requires an Application instance.
      IDTSComponentMetaData100 component1 = dataFlowTask.ComponentMetaDataCollection.New();
      component1.Name = "DataReader Source";
      component1.ComponentClassID = app.PipelineComponentInfos["DataReader Source"].CreationName;

      // Add a second ADO NET source to the data flow.
      //  The AssemblyQualifiedName property requires a reference to the assembly.
      IDTSComponentMetaData100 component2 = dataFlowTask.ComponentMetaDataCollection.New();
      component2.Name = "DataReader Source";
      component2.ComponentClassID = typeof(Microsoft.SqlServer.Dts.Pipeline.DataReaderSourceAdapter).AssemblyQualifiedName;
    }
  }
}
Imports Microsoft.SqlServer.Dts.Runtime
Imports Microsoft.SqlServer.Dts.Pipeline
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper

Module Module1

  Sub Main()

    Dim package As Microsoft.SqlServer.Dts.Runtime.Package = _
      New Microsoft.SqlServer.Dts.Runtime.Package()
    Dim e As Executable = package.Executables.Add("STOCK:PipelineTask")
    Dim thMainPipe As Microsoft.SqlServer.Dts.Runtime.TaskHost = _
      CType(e, Microsoft.SqlServer.Dts.Runtime.TaskHost)
    Dim dataFlowTask As MainPipe = CType(thMainPipe.InnerObject, MainPipe)

    ' The Application object will be used to obtain the CreationName
    '  of a PipelineComponentInfo from its PipelineComponentInfos collection.
    Dim app As New Application()

    ' Add a first ADO NET source to the data flow.
    '  The CreationName property requires an Application instance.
    Dim component1 As IDTSComponentMetaData100 = _
      dataFlowTask.ComponentMetaDataCollection.New()
    component1.Name = "DataReader Source"
    component1.ComponentClassID = app.PipelineComponentInfos("DataReader Source").CreationName

    ' Add a second ADO NET source to the data flow.
    '  The AssemblyQualifiedName property requires a reference to the assembly.
    Dim component2 As IDTSComponentMetaData100 = _
      dataFlowTask.ComponentMetaDataCollection.New()
    component2.Name = "DataReader Source"
    component2.ComponentClassID = _
      GetType(Microsoft.SqlServer.Dts.Pipeline.DataReaderSourceAdapter).AssemblyQualifiedName

  End Sub

End Module

建立元件的設計階段執行個體

呼叫 Instantiate 方法以建立 ComponentClassID 屬性所識別的元件之設計階段執行個體。此方法會傳回 CManagedComponentWrapper 物件,它是 IDTSDesigntimeComponent100 介面的 Managed 包裝函式。

請盡可能使用設計階段執行個體的方法來修改元件,而不要採用直接修改元件中繼資料的方式。雖然在中繼資料中有一些項目是必須直接設定的 (例如連接),不過,通常不建議直接修改中繼資料,因為您會略過元件監視並驗證變更的能力。

指派連接

有些元件 (例如 OLE DB 來源元件) 需要外部資料的連接,並為了此用途使用封裝中現有的 ConnectionManager 物件。RuntimeConnectionCollection 集合的 Count 屬性會指出元件所需的執行階段 ConnectionManager 物件數目。如果計數大於零,則元件需要連接。藉由指定 RuntimeConnectionCollection 內第一個連接的 ConnectionManagerName 屬性,將連接管理員從封裝指派至元件。請注意,執行階段連接集合中的連接管理員名稱,必須符合從封裝中參考的連接管理員名稱。

設定自訂屬性值

在建立元件的設計階段執行個體之後,呼叫 ProvideComponentProperties 方法。此方法類似於建構函式,因為它會建立其自訂屬性及其輸入和輸出物件,以初始化新建立的元件。請勿在一個元件上呼叫 ProvideComponentProperties 超過一次,因為元件可能會重設本身,並且遺失之前對其中繼資料所做的任何修改。

元件的 CustomPropertyCollection 包含該元件特有的 IDTSCustomProperty100 物件之集合。與其他程式設計模型不同的是,物件的屬性在物件上永遠是可見的,而元件只會在您呼叫 ProvideComponentProperties 方法時,擴展其自訂屬性集合。在您呼叫方法之後,請使用元件設計階段執行個體的 SetComponentProperty 方法,將值指派到其自訂屬性。此方法會接受識別自訂屬性的名稱/值組,並提供它的新值。

初始化輸出資料行

在您將元件加入工作並設定它之後,會初始化物件的 IDTSOutput100 中之資料行集合。這個步驟對於來源元件特別重要,但是可能無法初始化轉換與目的地元件的資料行,因為它們通常與從上游元件收到的資料行相依。

呼叫 ReinitializeMetaData 方法以初始化來源元件輸出中的資料行。因為元件不會自動連接到外部資料來源,所以請在呼叫 ReinitializeMetaData 之前先呼叫 AcquireConnections 方法,以提供其外部資料來源的元件存取權,以及擴展其資料行中繼資料的能力。最後,呼叫 ReleaseConnections 方法以釋放連接。

下一個步驟

在加入和設定元件之後,下一個步驟就是要在兩個元件之間建立路徑,也就是在兩個元件之間建立路徑主題所討論的內容。

範例

下列程式碼範例會將 OLE DB 來源元件加入資料流程工作,建立元件的設計階段執行個體,然後設定元件的屬性。這個範例需要 Microsoft.SqlServer.DTSRuntimeWrap 組件的額外參考。

using System;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
using Microsoft.SqlServer.Dts.Pipeline;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;

namespace Microsoft.SqlServer.Dts.Samples
{
  class Program
  {
    static void Main(string[] args)
    {
      Runtime.Package package = new Runtime.Package();
      Executable e = package.Executables.Add("STOCK:PipelineTask");
      Runtime.TaskHost thMainPipe = e as Runtime.TaskHost;
      MainPipe dataFlowTask = thMainPipe.InnerObject as MainPipe;
      
      // Add an OLEDB connection manager to the package.
      ConnectionManager cm = package.Connections.Add("OLEDB");
      cm.Name = "OLEDB ConnectionManager";
      cm.ConnectionString = "Data Source=(local);" + 
        "Initial Catalog=AdventureWorks2008R2;Provider=SQLNCLI11.1;" + 
        "Integrated Security=SSPI;"
      
      // Add an OLE DB source to the data flow.
      IDTSComponentMetaData100 component = 
        dataFlowTask.ComponentMetaDataCollection.New();
      component.Name = "OLEDBSource";
      component.ComponentClassID = "DTSAdapter.OleDbSource.2";
      // You can also use the CLSID of the component instead of the PROGID.
      //component.ComponentClassID = "{2C0A8BE5-1EDC-4353-A0EF-B778599C65A0}";

      // Get the design time instance of the component.
      CManagedComponentWrapper instance = component.Instantiate();

      // Initialize the component
      instance.ProvideComponentProperties();

      // Specify the connection manager.
      if (component.RuntimeConnectionCollection.Count > 0)
      {
        component.RuntimeConnectionCollection[0].ConnectionManager = 
          DtsConvert.GetExtendedInterface(package.Connections[0]);
        component.RuntimeConnectionCollection[0].ConnectionManagerID = 
          package.Connections[0].ID;      }

      // Set the custom properties.
      instance.SetComponentProperty("AccessMode", 2);
      instance.SetComponentProperty("SqlCommand", 
        "Select * from Production.Product");

      // Reinitialize the metadata.
      instance.AcquireConnections(null);
      instance.ReinitializeMetaData();
      instance.ReleaseConnections();

      // Add other components to the data flow and connect them.
    }
  }
}
Imports Microsoft.SqlServer.Dts.Runtime
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper
Imports Microsoft.SqlServer.Dts.Pipeline
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper

Module Module1

  Sub Main()

    Dim package As Microsoft.SqlServer.Dts.Runtime.Package = _
      New Microsoft.SqlServer.Dts.Runtime.Package()
    Dim e As Executable = package.Executables.Add("STOCK:PipelineTask")
    Dim thMainPipe As Microsoft.SqlServer.Dts.Runtime.TaskHost = _
      CType(e, Microsoft.SqlServer.Dts.Runtime.TaskHost)
    Dim dataFlowTask As MainPipe = CType(thMainPipe.InnerObject, MainPipe)

    ' Add an OLEDB connection manager to the package.
    Dim cm As ConnectionManager = package.Connections.Add("OLEDB")
    cm.Name = "OLEDB ConnectionManager"
    cm.ConnectionString = "Data Source=(local);" & _
      "Initial Catalog=AdventureWorks2008R2;Provider=SQLCLI11.1;" & _
      "Integrated Security=SSPI;"

    ' Add an OLE DB source to the data flow.
    Dim component As IDTSComponentMetaData100 = _
      dataFlowTask.ComponentMetaDataCollection.New()
    component.Name = "OLEDBSource"
    component.ComponentClassID = "DTSAdapter.OleDbSource.2"
    ' You can also use the CLSID of the component instead of the PROGID.
    'component.ComponentClassID = "{2C0A8BE5-1EDC-4353-A0EF-B778599C65A0}";

    ' Get the design time instance of the component.
    Dim instance As CManagedComponentWrapper = component.Instantiate()

    ' Initialize the component.
    instance.ProvideComponentProperties()

    ' Specify the connection manager.
    If component.RuntimeConnectionCollection.Count > 0 Then
      component.RuntimeConnectionCollection(0).ConnectionManager = _
        DtsConvert.GetExtendedInterface(package.Connections(0))
      component.RuntimeConnectionCollection(0).ConnectionManagerID = _
        package.Connections(0).ID
    End If

    ' Set the custom properties.
    instance.SetComponentProperty("AccessMode", 2)
    instance.SetComponentProperty("SqlCommand", _
      "Select * from Production.Product")

    ' Reinitialize the metadata.
    instance.AcquireConnections(vbNull)
    instance.ReinitializeMetaData()
    instance.ReleaseConnections()

    ' Add other components to the data flow and connect them.

  End Sub

End Module

外部資源

Integration Services 圖示 (小) 掌握 Integration Services 的最新狀態

若要取得 Microsoft 的最新下載、文章、範例、影片,以及社群的精選解決方案,請瀏覽 MSDN 上的 Integration Services 頁面:


若要得到這些更新的自動通知,請訂閱該頁面上所提供的 RSS 摘要。

變更記錄

更新的內容

更新將 OLE DB 來源元件加入資料流程工作的範例,以參考 SQLNCLI11.1 提供者和 ComponentClassID 的 DTSAdapter.OleDbSource.2 值。