Share via


プログラムによるデータ フロー コンポーネントの追加

適用対象: SQL Server Azure Data Factory の SSIS Integration Runtime

データ フローを作成するには、最初にコンポーネントを追加します。 次に、追加したコンポーネントを構成して相互に接続し、実行時のデータ フローを確立します。 このセクションでは、データ フロー タスクへのコンポーネントの追加、コンポーネントのデザイン時インスタンスの作成、およびコンポーネントの構成について説明します。 コンポーネントの接続方法については、「プログラムによるデータ フロー コンポーネントの接続」を参照してください。

コンポーネントの追加

New コレクションの ComponentMetaDataCollection メソッドを呼び出して新しいコンポーネントを作成し、データ フロー タスクに追加します。 このメソッドは、コンポーネントの IDTSComponentMetaData100 インターフェイスを返します。 ただしこの時点で、IDTSComponentMetaData100 には、いずれかのコンポーネントに固有な情報は含まれていません。 ComponentClassID プロパティを設定し、コンポーネントの種類を識別します。 データ フロー タスクはこのプロパティの値を使用して、実行時にコンポーネントのインスタンスを作成します。

ComponentClassID プロパティの値には、CLSID、PROGID、またはコンポーネントの CreationName プロパティを指定できます。 CLSID は通常、コンポーネントの ComponentClassID プロパティの値として [プロパティ] ウィンドウに表示されます。 このプロパティ、および使用できるコンポーネントの他のプロパティの取得については、「プログラムによるデータ フロー コンポーネントの検出」を参照してください。

マネージド コンポーネントの追加

CLSID または PROGID を使用して、いずれかのマネージド データ フロー コンポーネントをデータ フローに追加することはできません。これらの値はコンポーネント自体ではなく、ラッパーを指しているためです。 代わりに、次のサンプルに示すように、CreationName プロパティまたは AssemblyQualifiedName プロパティを使用できます。

AssemblyQualifiedName プロパティを使用する場合は、Visual Studio プロジェクトで、マネージド コンポーネントを含んでいるアセンブリに参照を追加する必要があります。 これらのアセンブリは、[参照の追加] ダイアログ ボックスの [.NET] タブに一覧表示されません。 通常は、C:\Program Files\Microsoft SQL Server\100\DTS\PipelineComponents フォルダーを参照してアセンブリを見つける必要があります。

組み込みマネージド データ フロー コンポーネントの要素は次のとおりです。

  • ADO.NET ソース

  • XML ソース

  • DataReader 変換先

  • SQL Server Compact 変換先

  • スクリプト コンポーネント

次のコード サンプルは、マネージド コンポーネントをデータ フローに追加するための 2 つの方法を示しています。

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 オブジェクトを返します。

可能な限り、コンポーネントのメタデータを直接変更するのではなく、必ずデザイン時インスタンスのメソッドを使用して、コンポーネントを変更する必要があります。 メタデータには、接続など、直接設定する必要のあるアイテムがありますが、メタデータを直接変更すると、コンポーネントが変更を監視および検証する機能がバイパスされるため、一般的にはお勧めしません。

接続の割り当て

OLE DB ソース コンポーネントなどの一部のコンポーネントは外部データに接続する必要があるため、パッケージ内にある既存の ConnectionManager オブジェクトをこの目的で使用します。 Count コレクションの RuntimeConnectionCollection プロパティは、コンポーネントが必要とする、実行時の ConnectionManager オブジェクトの数を示します。 この数が 0 より大きい場合、コンポーネントには接続が必要です。 ConnectionManager の最初の接続の Name および RuntimeConnectionCollection プロパティを指定して、パッケージの接続マネージャーをコンポーネントに割り当てます。 実行時の接続コレクション内にある接続マネージャーの名前は、パッケージから参照される接続マネージャーの名前と一致する必要があります。

カスタム プロパティの値の設定

コンポーネントのデザイン時インスタンスを作成したら、ProvideComponentProperties メソッドを呼び出します。 このメソッドは、コンストラクターと同様、新しく作成されたコンポーネントのカスタム プロパティと入力および出力オブジェクトを作成することにより、そのコンポーネントを初期化します。 コンポーネントがリセットされ、メタデータに加えられた以前の変更が失われるため、1 つのコンポーネントで ProvideComponentProperties を複数回呼び出さないでください。

コンポーネントの CustomPropertyCollection には、コンポーネント固有の IDTSCustomProperty100 オブジェクトのコレクションが含まれています。 オブジェクトのプロパティが常に可視である他のプログラミング モデルとは異なり、コンポーネントは ProvideComponentProperties メソッドが呼び出されたときにのみ、そのコンポーネントのカスタム プロパティのコレクションを設定します。 メソッドを呼び出したら、コンポーネントのデザイン時インスタンスの SetComponentProperty メソッドを使用して、コンポーネントのカスタム プロパティに値を割り当てます。 このメソッドは、カスタム プロパティを識別する名前と値のペアを受け取り、新しい値を設定します。

出力列の初期化

コンポーネントをタスクに追加して構成したら、オブジェクトの IDTSOutput100 で列のコレクションを初期化します。 この手順は変換元コンポーネントに特に関連するものです。変換および変換先コンポーネントの列は、一般的に、上流コンポーネントから受け取る列に依存するため、この手順では初期化されない場合があります。

ReinitializeMetaData メソッドを呼び出し、変換元コンポーネントの出力の列を初期化します。 コンポーネントは外部データ ソースに自動的には接続されないので、AcquireConnections を呼び出す前に ReinitializeMetaData メソッドを呼び出して、コンポーネントに外部データ ソースへのアクセスを提供し、列のメタデータを設定します。 最後に ReleaseConnections メソッドを呼び出し、接続を解放します。

次の手順

コンポーネントを追加して構成したら、次の手順としてコンポーネント間のパスを作成します。これについては、トピック「2 つのコンポーネント間のパスの作成」で説明します。

サンプル

次のコード例は、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=AdventureWorks;Provider=SQLOLEDB.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.1";  
      // 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=AdventureWorks;Provider=SQLOLEDB.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.1"  
    ' 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  

外部リソース

blogs.msdn.com のブログ「EzAPI - Updated for SQL Server 2012」(EzAPI - SQL Server 2012 用の更新)

参照

プログラムによるデータ フロー コンポーネントの接続