ActiveConnection、CommandText、CommandTimeout、CommandType、Size 和 Direction 属性范例

该范例使用 ActiveConnectionCommandTextCommandTimeoutCommandTypeSizeDirection 属性执行存储过程。

Public Sub ActiveConnectionX()   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
   Set cmdByRoyalty = New ADODB.Command
   Set cmdByRoyalty.ActiveConnection = cnn1
   cmdByRoyalty.CommandText = "byroyalty"
   cmdByRoyalty.CommandType = adCmdStoredProc
   cmdByRoyalty.CommandTimeout = 15
      
   ' 定义存储过程的输入参数。
   intRoyalty = Trim(InputBox( _
      "Enter royalty:"))
   Set prmByRoyalty = New ADODB.Parameter
   prmByRoyalty.Type = adInteger
   prmByRoyalty.Size = 3
   prmByRoyalty.Direction = adParamInput
   prmByRoyalty.Value = intRoyalty
   cmdByRoyalty.Parameters.Append prmByRoyalty
  
   ' 通过执行该命令创建记录集。
   Set rstByRoyalty = cmdByRoyalty.Execute()
      
   ' 打开作者表以便显示作者姓名。
   Set rstAuthors = New ADODB.Recordset
   rstAuthors.Open "authors", strCnn, , , adCmdTable
   
   ' 打印记录集中的当前数据,从作者表中添加作者姓名。
   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.2mysite.net