다음을 통해 공유


명명된 명령

간단한 명령 만들고 실행하면 명령을 실행하는 한 가지 방법이 표시됩니다. 또 다른 방법이 있습니다. 명명된 명령으로 지정한 다음 Connection 개체에서 직접 이 명명된 명령을 호출할 수 있습니다(Command 개체의 ActiveConnection 속성에 할당됨). 명령 이름을 지정한다는 것은 Command 개체의 Name 속성에 이름을 할당하는 것을 의미합니다. 예를 들어

objCmd.Name = "GetCustomers"  
objCmd.ActiveConnection = objConn  
objConn.GetCustomers objRs  

명명된 명령은 Connection 개체의 "사용자 지정 메서드"인 것처럼 작동합니다. 명령의 결과는 이 "사용자 지정 메서드"의 out 매개 변수로 반환됩니다.

다음 예제에서는 이 기능을 보여 줍니다.

'BeginNamedCmd  
    On Error GoTo ErrHandler:  
  
    Dim objConn As New ADODB.Connection  
    Dim objCmd As New ADODB.Command  
    Dim objRs As New ADODB.Recordset  
  
    ' Connect to the data source.  
    Set objConn = GetNewConnection  
  
    objCmd.CommandText = "SELECT CustomerID, CompanyName FROM Customers"  
    objCmd.CommandType = adCmdText  
  
    'Name the command.  
    objCmd.Name = "GetCustomers"  
  
    objCmd.ActiveConnection = objConn  
  
    ' Execute using Command.Name from the Connection.  
    objConn.GetCustomers objRs  
  
    ' Display.  
    Do While Not objRs.EOF  
        Debug.Print objRs(0) & vbTab & objRs(1)  
        objRs.MoveNext  
    Loop  
  
    'clean up  
    objRs.Close  
    objConn.Close  
    Set objRs = Nothing  
    Set objConn = Nothing  
    Set objCmd = Nothing  
    Exit Sub  
  
ErrHandler:  
    'clean up  
    If objRs.State = adStateOpen Then  
        objRs.Close  
    End If  
  
    If objConn.State = adStateOpen Then  
        objConn.Close  
    End If  
  
    Set objRs = Nothing  
    Set objConn = Nothing  
    Set objCmd = Nothing  
  
    If Err <> 0 Then  
        MsgBox Err.Source & "-->" & Err.Description, , "Error"  
    End If  
'EndNamedCmd  

참고 항목

연결 개체 (ADO)