연결 개체의 메서드로 저장 프로시저 호출

연결된 열린 Connection 개체의 네이티브 메서드인 것처럼 저장 프로시저를 호출할 수 있습니다. Connection 개체에서 명명된 명령을 호출하는 것과 비슷합니다.

다음 Visual Basic 코드 예제에서는 편의상 여기에 다시 나열된 CustOrdersOrders라는 Northwind 샘플 데이터베이스의 저장 프로시저를 호출합니다.

CREATE PROCEDURE CustOrdersOrders @CustomerID nchar(5) AS  
SELECT OrderID, OrderDate, RequiredDate, ShippedDate  
FROM Orders  
WHERE CustomerID = @CustomerID  
ORDER BY OrderID  

다음 코드 예제에서는 연결된 열린 Connection 개체에서 네이티브 메서드인 것처럼 저장 프로시저를 호출하는 방법을 보여 줍니다.

Const DS = "MySQLServer"  
Const DB = "Northwind"  
Const DP = "SQLOLEDB"  
  
Dim objConn As New ADODB.Connection  
Dim objRs As New ADODB.Recordset  
Dim objComm As New ADODB.Command  
  
ConnectionString = "Provider=" & DP & _  
                   ";Data Source=" & DS & _  
                   ";Initial Catalog=" & DB & _  
                   ";Integrated Security=SSPI;"  
  
' Connect to the data source.  
objConn.Open ConnectionString  
  
' Set a stored procedure  
  
Set objComm.ActiveConnection = objConn  
  
' Execute the stored procedure on  
' the active connection object...  
'    "ALFKI" is the required input parameter,  
'    objRs is the resultant output variable.  
objComm(1) = "ALFKI"
Set objRs = objComm.Execute

' Display the result.  
Debug.Print "Results returned from sp_CustOrdersOrders for ALFKI: "  
Do While Not objRs.EOF  
    Debug.Print vbTab & objRs(0) & vbTab & objRs(1) & vbTab & _  
                objRs(2) & vbTab & objRs(3)  
    objRs.MoveNext  
 Loop  
  
'Clean up.  
objRs.Close  
objConn.Close  
Set objRs = Nothing  
Set objConn = Nothing  
Set objComm = Nothing  

참고 항목

연결 개체(ADO)