共用方式為


使用命令呼叫預存程式

您可以使用 命令來呼叫預存程式。 本主題結尾的程式代碼範例是指 Northwind 範例資料庫中稱為 CustOrdersOrders 的預存程式,其定義如下。

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

如需如何定義和呼叫預存程式的詳細資訊,請參閱 SQL Server 檔。

這個預存程式類似於 Command Object Parameters中使用的命令。 它會採用客戶標識子參數,並傳回該客戶訂單的相關信息。 下列程式代碼範例會使用此預存程式作為 ADO Recordset的來源。

使用預存程式可讓您存取 ADO 的另一項功能:Parameters 集合 Refresh 方法。 藉由使用此方法,ADO 可以在運行時間自動填入命令所需參數的所有資訊。 使用這項技術會降低效能,因為 ADO 必須查詢數據源以取得參數的相關信息。

下列程式代碼範例與 Command Object Parameters中的程式碼之間存在其他重要差異,其中已手動輸入參數。 首先,此程式代碼不會將 Prepared 屬性設定為 True,因為它是 SQL Server 預存程式,而且已依定義先行編譯。 其次,第二個範例中 Command 物件的 CommandType 屬性已變更為 adCmdStoredProc,以通知 ADO 命令是預存程式。

最後,在第二個範例中,參數必須在設定值時由索引參考,因為您可能不知道設計時間的參數名稱。 如果您知道參數的名稱,您可以將 Command 物件的 new NamedParameters 屬性設定為 True,並參考屬性的名稱。 您可能會想知道為什麼預存程式 (@CustomerID) 中所提及的第一個參數位置是 1,而不是 0 (objCmd(1) = "ALFKI")。 這是因為參數 0 包含來自 SQL Server 預存程式的傳回值。

'BeginAutoParamCmd  
    On Error GoTo ErrHandler:  
  
    Dim objConn As New ADODB.Connection  
    Dim objCmd As New ADODB.Command  
    Dim objParm1 As New ADODB.Parameter  
    Dim objRs As New ADODB.Recordset  
  
    ' Set CommandText equal to the stored procedure name.  
    objCmd.CommandText = "CustOrdersOrders"  
    objCmd.CommandType = adCmdStoredProc  
  
    ' Connect to the data source.  
    Set objConn = GetNewConnection  
    objCmd.ActiveConnection = objConn  
  
    ' Automatically fill in parameter info from stored procedure.  
    objCmd.Parameters.Refresh  
  
    ' Set the param value.  
    objCmd(1) = "ALFKI"  
  
    ' Execute once and display...  
    Set objRs = objCmd.Execute  
  
    Debug.Print objParm1.Value  
    Do While Not objRs.EOF  
        Debug.Print vbTab & objRs(0) & vbTab & objRs(1) & vbTab & _  
                    objRs(2) & vbTab & objRs(3)  
        objRs.MoveNext  
    Loop  
  
    ' ...then set new param value, re-execute command, and display.  
    objCmd(1) = "CACTU"  
    Set objRs = objCmd.Execute  
  
    Debug.Print objParm1.Value  
    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 objCmd = Nothing  
    Set objParm1 = 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  
    Set objParm1 = Nothing  
  
    If Err <> 0 Then  
        MsgBox Err.Source & "-->" & Err.Description, , "Error"  
    End If  
'EndAutoParamCmd  
  
'BeginNewConnection  
Private Function GetNewConnection() As ADODB.Connection  
    Dim oCn As New ADODB.Connection  
    Dim sCnStr As String  
  
    sCnStr = "Provider='SQLOLEDB';Data Source='MySqlServer';" & _  
             "Integrated Security='SSPI';Initial Catalog='Northwind';"  
    oCn.Open sCnStr  
  
    If oCn.State = adStateOpen Then  
        Set GetNewConnection = oCn  
    End If  
  
End Function  
'EndNewConnection  

另請參閱

知識庫文章 117500