适用于:Access 2013、Office 2013
以下示例显示了 Command 对象的一个更有趣的用法,其中 SQL 命令的文本已经修改为使其可以参数化。 这样就可以重复使用命令,每次传递参数的不同值。 由于 Command 对象的 Prepared 属性设置为 True ,ADO 将要求提供程序在首次执行 CommandText 中指定的命令前编译该命令。 它还会将编译后的命令保留在内存中。 由于准备工作需要开销,这会略微减慢命令首次执行速度,但以后每次调用该命令时会有一个性能增益。 因此,只有在多次使用命令时才应准备命令。
'BeginManualParamCmd
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 the CommandText as a parameterized SQL query.
objCmd.CommandText = "SELECT OrderID, OrderDate, " & _
"RequiredDate, ShippedDate " & _
"FROM Orders " & _
"WHERE CustomerID = ? " & _
"ORDER BY OrderID"
objCmd.CommandType = adCmdText
' Prepare command since we will be executing it more than once.
objCmd.Prepared = True
' Create new parameter for CustomerID. Initial value is ALFKI.
Set objParm1 = objCmd.CreateParameter("CustId", adChar, _
adParamInput, 5, "ALFKI")
objCmd.Parameters.Append objParm1
' Connect to the data source.
Set objConn = GetNewConnection
objCmd.ActiveConnection = objConn
' 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("CustId") = "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
'EndManualParamCmd
并不是所有提供程序都支持准备命令。 如果提供程序不支持命令准备,则在将此属性设置为 True 后会立即返回一个错误。 如果不返回错误,它会忽略准备命令的请求,并将 Prepared 属性设置为 False 。