Append 和 CreateParameter 方法范例

该范例使用 Append 和 CreateParameter 方法执行具有输入参数的存储过程。

Public Sub AppendX()   Dim cnn1 As ADODB.Connection
   Dim cmdByRoyalty As ADODB.Command
   Dim prmByRoyalty As ADODB.Parameter
   Dim rstByRoyalty As ADODB.Recordset
   Dim rstAuthors As ADODB.Recordset
   Dim intRoyalty As Integer
   Dim strAuthorID As String
   Dim strCnn As String   ' 打开连接。
   Set cnn1 = New ADODB.Connection
   strCnn = "Provider=sqloledb;" & _
      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
   cnn1.Open strCnn
   cnn1.CursorLocation = adUseClient
      
   ' 使用一个参数打开命令对象。
   Set cmdByRoyalty = New ADODB.Command
   cmdByRoyalty.CommandText = "byroyalty"
   cmdByRoyalty.CommandType = adCmdStoredProc
   
   ' 获取参数值并追加参数。
   intRoyalty = Trim(InputBox("Enter royalty:"))
   Set prmByRoyalty = cmdByRoyalty.CreateParameter("percentage", _
      adInteger, adParamInput)
   cmdByRoyalty.Parameters.Append prmByRoyalty
   prmByRoyalty.Value = intRoyalty   ' 通过执行命令创建记录集。
   Set cmdByRoyalty.ActiveConnection = cnn1
   Set rstByRoyalty = cmdByRoyalty.Execute
   
   ' 打开 Authors 表以获取作者姓名进行显示。
   Set rstAuthors = New ADODB.Recordset
   rstAuthors.Open "authors", cnn1, , , adCmdTable
   
   ' 打印记录集中的当前数据,从 Authors 表中添加作者姓名。
   Debug.Print "Authors with " & intRoyalty & " percent royalty"
   Do While Not rstByRoyalty.EOF
      strAuthorID = rstByRoyalty!au_id
      Debug.Print "   " & rstByRoyalty!au_id & ", ";
      rstAuthors.Filter = "au_id = '" & strAuthorID & "'"
      Debug.Print rstAuthors!au_fname & " " & rstAuthors!au_lname
      rstByRoyalty.MoveNext
   Loop   rstByRoyalty.Close
   rstAuthors.Close
   cnn1.CloseEnd Sub
www.51windows.Net