CacheSize 属性范例

该范例使用 CacheSize 属性,说明当使用和不使用记录数为 30 的缓存时,所执行的操作在性能上的差别。

Public Sub CacheSizeX()

   Dim rstRoySched As ADODB.Recordset

   Dim strCnn As String

   Dim sngStart As Single

   Dim sngEnd As Single

   Dim sngNoCache As Single

   Dim sngCache As Single

   Dim intLoop As Integer

   Dim strTemp As String

   ' 打开 RoySched 表。

   strCnn = "Provider=sqloledb;" & _

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

   Set rstRoySched = New ADODB.Recordset

   rstRoySched.Open "roysched", strCnn, , , adCmdTable

   ' 枚举记录集对象两次并记录经历的时间。

   sngStart = Timer

   For intLoop = 1 To 2

      rstRoySched.MoveFirst

      Do While Not rstRoySched.EOF

         ' Execute a simple operation for the

         ' performance test.

         strTemp = rstRoySched!title_id

         rstRoySched.MoveNext

      Loop

   Next intLoop

   sngEnd = Timer

   sngNoCache = sngEnd - sngStart

   ' 以 30 个记录为一组缓存记录。

   rstRoySched.MoveFirst

   rstRoySched.CacheSize = 30

   sngStart = Timer

   ' 枚举记录集对象两次并记录经历的时间。

   For intLoop = 1 To 2

      rstRoySched.MoveFirst

      Do While Not rstRoySched.EOF

         ' 执行一个简单操作,进行性能测试。

         strTemp = rstRoySched!title_id

         rstRoySched.MoveNext

      Loop

   Next intLoop

   sngEnd = Timer

   sngCache = sngEnd - sngStart

   ' 显示性能结果。

   MsgBox "Caching Performance Results:" & vbCr & _

      "   No cache: " & Format(sngNoCache, _

      "##0.000") & " seconds" & vbCr & _

      "   30-record cache: " & Format(sngCache, _

      "##0.000") & " seconds"

   rstRoySched.Close

End Sub