사용자 지정 태스크에서 데이터 원본에 연결Connecting to Data Sources in a Custom Task

태스크는 연결 관리자를 통해 외부 데이터 원본에 연결하여 데이터를 검색하거나 저장합니다.Tasks connect to external data sources to retrieve or save data by using a connection manager. 디자인 타임에 연결 관리자는 논리적 연결을 나타내며 서버 이름 및 인증 속성과 같은 주요 정보를 설명합니다.At design time, a connection manager represents a logical connection, and describes key information such as the server name and any authentication properties. 런타임에 태스크에서는 연결 관리자의 AcquireConnection 메서드를 호출하여 데이터 원본에 대한 실제 연결을 설정합니다.At run time, tasks call the AcquireConnection method of the connection manager to establish the physical connection to the data source.

패키지에는 각기 다른 데이터 원본에 연결하는 여러 태스크가 있을 수 있으므로 패키지에서는 모든 연결 관리자를 Connections라는 컬렉션에서 추적합니다.Because a package can contain many tasks, each of which may have connections to different data sources, the package tracks all the connection managers in a collection, the Connections collection. 태스크에서는 해당 패키지의 컬렉션을 사용하여 유효성 검사 및 실행 중 사용할 연결 관리자를 찾습니다.Tasks use the collection in their package to find the connection manager that they will use during validation and execution. Connections 컬렉션은 ValidateExecute 메서드에 대한 첫 번째 매개 변수입니다.The Connections collection is the first parameter to the Validate and Execute methods.

컬렉션의 ConnectionManager 개체를 사용자에게 표시하거나 그래픽 사용자 인터페이스의 대화 상자 또는 드롭다운 목록을 사용하여 태스크에서 잘못된 연결 관리자를 사용하지 못하도록 할 수 있습니다.You can prevent the task from using the wrong connection manager by displaying the ConnectionManager objects from the collection to the user, by using a dialog box or drop-down list in the graphical user interface. 이렇게 하면 사용자는 패키지에 포함된 적절한 유형의 ConnectionManager 개체 중에서만 선택할 수 있습니다.This gives the user a way to select from among only those ConnectionManager objects of the appropriate type that are contained in the package.

태스크에서는 AcquireConnection 메서드를 호출하여 데이터 원본에 대한 실제 연결을 설정합니다.Tasks call the AcquireConnection method to establish the physical connection to the data source. 이 메서드는 태스크에서 사용될 수 있는 기본 연결 개체를 반환합니다.The method returns the underlying connection object that can then be used by the task. 연결 관리자는 기본 연결 개체의 구현 세부 사항을 태스크에서 격리하므로 태스크에서는 AcquireConnection 메서드를 호출하여 연결을 설정하기만 하면 되고 연결의 다른 측면은 고려할 필요가 없습니다.Because the connection manager isolates the implementation details of the underlying connection object from the task, the task only has to call the AcquireConnection method to establish the connection, and does not have to be concerned with other aspects of the connection.

예제Example

다음 예제 코드에서는 Validate 및 Execute 메서드에서 ConnectionManager 이름의 유효성을 검사하는 방법과 Execute 메서드에서 AcquireConnection 메서드를 사용하여 실제 연결을 설정하는 방법을 보여 줍니다.The following sample code demonstrates validation of the ConnectionManager name in the Validate and Execute methods, and shows how to use the AcquireConnection method to establish the physical connection in the Execute method.

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  

참고 항목See Also

Integration Services ( Ssis) 연결 Integration Services (SSIS) Connections
연결 관리자 만들기Create Connection Managers