次の方法で共有


カスタム タスクでのデータ ソースへの接続

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

タスクは、接続マネージャーを使用して外部データ ソースに接続し、データを取得または保存します。 デザイン時には、接続マネージャーは論理接続を表し、サーバー名や認証プロパティなどの重要な情報を示します。 実行時に、タスクは接続マネージャーの AcquireConnection メソッドを呼び出して、データ ソースへの物理接続を確立します。

パッケージに多数のタスクが含まれていると、各タスクに異なるデータ ソースへの接続が含まれる場合があるため、パッケージは Connections コレクション内のすべての接続マネージャーを監視します。 タスクはパッケージ内にあるコレクションを使用し、検証および実行中に使用する接続マネージャーを検索します。 Connections コレクションは、Validate および Execute メソッドの最初のパラメーターです。

グラフィカル ユーザー インターフェイスのダイアログ ボックスまたはドロップダウン リストを使用して、コレクションの ConnectionManager オブジェクトをユーザーに表示すると、タスクで間違った接続マネージャーが使用されないようにすることができます。 これによりユーザーは、パッケージ内に含まれる適切な型の ConnectionManager オブジェクトのみから選択できるようになります。

タスクは、AcquireConnection メソッドを呼び出し、データ ソースに対する物理接続を確立します。 メソッドは基になる接続オブジェクトを返し、タスクはその接続オブジェクトを使用できます。 接続マネージャーは、基になる接続オブジェクトの実装の詳細をタスクから分離します。そのため、接続を確立するには、タスクは AcquireConnection メソッドを呼び出すだけでよく、接続の他の側面を考慮する必要はありません。

次の例では、Validate および Execute メソッドで ConnectionManager 名を検証し、AcquireConnection メソッドを使用して Execute メソッド内に物理接続を確立する方法を示します。

private string connectionManagerName = "";  
  
public string ConnectionManagerName  
{  
  get { return this.connectionManagerName; }  
  set { this.connectionManagerName = value; }  
}  
  
public override DTSExecResult Validate(  
  Connections connections, VariableDispenser variableDispenser,  
  IDTSComponentEvents componentEvents, IDTSLogging log)  
{  
  // If the connection manager exists, validation is successful;  
  // otherwise, fail validation.  
  try  
  {  
    ConnectionManager cm = connections[this.connectionManagerName];  
    return DTSExecResult.Success;  
  }  
  catch (System.Exception e)  
  {  
    componentEvents.FireError(0, "SampleTask", "Invalid connection manager.", "", 0);  
    return DTSExecResult.Failure;  
  }  
}  
  
public override DTSExecResult Execute(Connections connections,   
  VariableDispenser variableDispenser, IDTSComponentEvents componentEvents,   
  IDTSLogging log, object transaction)  
{  
  try  
  {  
    ConnectionManager cm = connections[this.connectionManagerName];  
    object connection = cm.AcquireConnection(transaction);  
    return DTSExecResult.Success;  
  }  
  catch (System.Exception exception)  
  {  
    componentEvents.FireError(0, "SampleTask", exception.Message, "", 0);  
    return DTSExecResult.Failure;  
  }  
}  
Private _connectionManagerName As String = ""  
  
Public Property ConnectionManagerName() As String  
  Get  
    Return Me._connectionManagerName  
  End Get  
  Set(ByVal Value As String)  
    Me._connectionManagerName = value  
  End Set  
End Property  
  
Public Overrides Function Validate( _  
  ByVal connections As Microsoft.SqlServer.Dts.Runtime.Connections, _  
  ByVal variableDispenser As Microsoft.SqlServer.Dts.Runtime.VariableDispenser, _  
  ByVal componentEvents As Microsoft.SqlServer.Dts.Runtime.IDTSComponentEvents, _  
  ByVal log As Microsoft.SqlServer.Dts.Runtime.IDTSLogging) _  
  As Microsoft.SqlServer.Dts.Runtime.DTSExecResult  
  
  ' If the connection manager exists, validation is successful;  
  ' otherwise fail validation.  
  Try  
    Dim cm As ConnectionManager = connections(Me._connectionManagerName)  
    Return DTSExecResult.Success  
  Catch e As System.Exception  
    componentEvents.FireError(0, "SampleTask", "Invalid connection manager.", "", 0)  
    Return DTSExecResult.Failure  
  End Try  
  
End Function  
  
Public Overrides Function Execute( _  
  ByVal connections As Microsoft.SqlServer.Dts.Runtime.Connections, _  
  ByVal variableDispenser As Microsoft.SqlServer.Dts.Runtime.VariableDispenser, _  
  ByVal componentEvents As Microsoft.SqlServer.Dts.Runtime.IDTSComponentEvents, _  
  ByVal log As Microsoft.SqlServer.Dts.Runtime.IDTSLogging, ByVal transaction As Object) _  
  As Microsoft.SqlServer.Dts.Runtime.DTSExecResult  
  
  Try  
    Dim cm As ConnectionManager = connections(Me._connectionManagerName)  
    Dim connection As Object = cm.AcquireConnection(transaction)  
    Return DTSExecResult.Success  
  Catch exception As System.Exception  
    componentEvents.FireError(0, "SampleTask", exception.Message, "", 0)  
    Return DTSExecResult.Failure  
  End Try  
  
End Function  

参照

Integration Services (SSIS) の接続
接続マネージャーを作成する