以 Connection 物件方法的形式來呼叫預存程序
您可以呼叫預存程序,好像其是相關聯開放 Connection 物件上的原生方法一樣。 這類似於在 Connection 物件上呼叫具名命令。
下列 Visual Basic 程式碼範例會呼叫 Northwind 範例資料庫中稱為 CustOrdersOrders 的預存程序,為了方便起見,這裡再次列出。
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