ActiveCommand 属性应用实例(VB)
本范例演示 ActiveCommand 属性。
为子例程提供了一个 Recordset 对象,该对象的 ActiveCommand 属性用于显示创建 Recordset 的命令文本和参数。
Public Sub Main()
ActiveCommandX
End Sub
Public Sub ActiveCommandX()
Dim cnn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rst As New ADODB.Recordset
Dim strPrompt As String, strName As String
strPrompt = "Enter an author's name (e.g., Ringer): "
strName = Trim(InputBox(strPrompt, "ActiveCommandX Example"))
cmd.CommandText = "SELECT * FROM Authors WHERE au_lname = ?"
cmd.Parameters.Append _
cmd.CreateParameter("LastName", adChar, adParamInput, 20, strName)
cnn.Open "DSN=Pubs;Provider=MSDASQL; uid=sa; pwd=;"
cmd.ActiveConnection = cnn
Set rst = cmd.Execute(, , adCmdText)
ActiveCommandXprint rst
rst.Close
cnn.Close
End Sub
对 ActiveCommandXprint 例程仅提供一个 Recordset 对象,而例程必须打印创建 Recordset 的命令文本和参数。由于 Recordset 对象的 ActiveCommand 属性输出相关的 Command 对象,所以可以这么做。
Command 对象的 CommandText 属性输出创建 Recordset 的参数化命令。Command 对象的 Parameters 集合输出替代命令参数占位符(“?”)的值。
最后将打印错误消息或作者的名字和 ID。
Public Sub ActiveCommandXprint(rstp As ADODB.Recordset)
Dim strName As String
strName = rstp.ActiveCommand.Parameters.Item("LastName").Value
Debug.Print "Command text = '"; rstp.ActiveCommand.CommandText; "'"
Debug.Print "Parameter = '"; strName; "'"
If rstp.BOF = True Then
Debug.Print "Name = '"; strName; "', not found."
Else
Debug.Print "Name = '"; rstp!au_fname; " "; rstp!au_lname; _
"', author ID = '"; rstp!au_id; "'"
End If
End Sub
0