此頁(yè)沒(méi)有內(nèi)容條目
內(nèi)容

Refresh 方法范例 (Visual Basic)

該范例說(shuō)明如何使用 Refresh 方法刷新已存儲(chǔ)過(guò)程 Command 對(duì)象的 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

   ' 打開(kāi)連接。

   Set cnn1 = New ADODB.Connection

      strCnn = "Provider=sqloledb;" & _

      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "

   cnn1.Open strCnn

   ' 使用一個(gè)參數(shù)打開(kāi)已存儲(chǔ)過(guò)程的命令對(duì)象。

   Set cmdByRoyalty = New ADODB.Command

   Set cmdByRoyalty.ActiveConnection = cnn1

   cmdByRoyalty.CommandText = "byroyalty"

   cmdByRoyalty.CommandType = adCmdStoredProc

   cmdByRoyalty.Parameters.Refresh

   ' 獲取參數(shù)值并執(zhí)行命令,將結(jié)果存儲(chǔ)到記錄集中。

   intRoyalty = Trim(InputBox("Enter royalty:"))

   cmdByRoyalty.Parameters(1) = intRoyalty

   Set rstByRoyalty = cmdByRoyalty.Execute()

   ' 打開(kāi) Authors 表獲取作者姓名以用于顯示。

   Set rstAuthors = New ADODB.Recordset

   rstAuthors.Open "authors", cnn1, , , adCmdTable

   ' 打印記錄集中的當(dāng)前數(shù)據(jù),添加 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