Refresh 方法范例 (Visual Basic)

该范例说明如何使用 Refresh 方法刷新已存储过程 Command 对象的 Parameters 集合。

Public Sub RefreshX()   Dim cnn1 As ADODB.Connection
   Dim cmdByRoyalty As ADODB.Command
   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
      
   ' 使用一个参数打开已存储过程的命令对象。
   Set cmdByRoyalty = New ADODB.Command
   Set cmdByRoyalty.ActiveConnection = cnn1
   cmdByRoyalty.CommandText = "byroyalty"
   cmdByRoyalty.CommandType = adCmdStoredProc
   cmdByRoyalty.Parameters.Refresh
   
   ' 获取参数值并执行命令,将结果存储到记录集中。
   intRoyalty = Trim(InputBox("Enter royalty:"))
   cmdByRoyalty.Parameters(1) = intRoyalty
   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.Close
   
End Sub
www.51windows.Net