Prepared 属性范例

该范例通过打开两个 Command 对象(一个是准备好的,另一个是未准备好的)演示 Prepared 属性。

Public Sub PreparedX()   Dim cnn1 As ADODB.Connection
   Dim cmd1 As ADODB.Command
   Dim cmd2 As ADODB.Command
   Dim strCnn As String
   Dim strCmd As String
   Dim sngStart As Single
   Dim sngEnd As Single
   Dim sngNotPrepared As Single
   Dim sngPrepared As Single
   Dim intLoop As Integer   ' 打开连接。
   strCnn = "Provider=sqloledb;" & _
      "Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
   Set cnn1 = New ADODB.Connection
   cnn1.Open strCnn   ' 为相同命令(一个是准备好的,另一个是未准备好的)创建两个命令对象。
   strCmd = "SELECT title, type FROM titles ORDER BY type"
   
   Set cmd1 = New ADODB.Command
   Set cmd1.ActiveConnection = cnn1
   cmd1.CommandText = strCmd
      
   Set cmd2 = New ADODB.Command
   Set cmd2.ActiveConnection = cnn1
   cmd2.CommandText = strCmd
   cmd2.Prepared = True
   
   ' 设置一个计时器,而后执行未准备好的命令 20 次。
   sngStart = Timer
   For intLoop = 1 To 20
      cmd1.Execute
   Next intLoop
   sngEnd = Timer
   sngNotPrepared = sngEnd - sngStart
   
   ' 重置计时器,而后执行准备好的命令 20 次。
   sngStart = Timer
   For intLoop = 1 To 20
      cmd2.Execute
   Next intLoop
   sngEnd = Timer
   sngPrepared = sngEnd - sngStart   ' 显示执行结果。
   MsgBox "Performance Results:" & vbCr & _
      "   Not Prepared: " & Format(sngNotPrepared, _
      "##0.000") & " seconds" & vbCr & _
      "   Prepared: " & Format(sngPrepared, _
      "##0.000") & " seconds"
      
   cnn1.CloseEnd Sub
www.51windows.Net