在连接对象上作为方法调用存储过程
可像关联的已打开的 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