How To Use the ADO FetchProgress and FetchComplete Events
来源:互联网 发布:2017php免杀一句话 编辑:程序博客网 时间:2024/06/08 09:59
Prerequisites
Software Requirements- The FetchProgress and FetchComplete events only work properly in MDAC 2.5 or later. You can download the latest version of the Microsoft Data Access Components from the following Microsoft Web site: Microsoft Data Access Components (http://msdn.microsoft.com/library/default.asp?url=/downloads/list/dataaccess.asp)
- If you are developing your application in Microsoft Visual Studio 6.0, then you must have service pack 3 or later installed. You can install the latest service pack for Microsoft Visual Studio 6.0 from the following Microsoft Web site: Visual Studio Product Updates (http://msdn.microsoft.com/vstudio/downloads/updates.asp)
- When you open the recordset, you must specify adAsyncFetch as a Recordset Option.
- You must use client-side cursors, because the FetchProgress and FetchComplete events are returned by the ActiveX Data Objects (ADO) client cursor engine.
- In Visual Basic, you must declare the Recordset at module level using Dim with the WithEvents keyword.
Properties
There are two recordset properties that affect the asynchronous behavior of ADO. These properties should be set prior to opening the recordset.- Initial Fetch Size determines how many records are fetched synchronously before the asynchronous thread is created. This allows for a small recordset to be created without the additional overhead of creating a thread. This is set to 50 by default. To guarantee that FetchProgress and FetchComplete are called, set this value to 0.
- Background Fetch Size determines how many records are fetched between calls to the FetchProgress event. This is set to 15 by default.
The Events
FetchProgressFetchProgress has four Parameters:
- Progress is the number of records currently in the recordset.
The first time FetchProgress is called, Progress is equal to Initial Fetch Size plus Background Fetch Size. For each additional call, Progress equals the previous value plus Background Fetch Size. - MaxProgress is the maximum value expected to be returned.
MaxProgress is not equal to the actual number of records that will be returned. ADO has to fetch the records in order to get this value. This means MaxProgress is only ever a best guess. MaxProgress usually equals Progress plus Background Fetch Size.
FetchProgess is always called before calling FetchComplete. In this case, Progress and MaxProgress are equal to each other. This MaxProgress equals the actual number of records retrieved. - The value of adStatus determines if any errors have occurred. This value is normally adStatusOK.
You may disable the FetchProgress event by setting the value of adStatus to the value adStatusUnwantedEvent. - pRecordset is a reference to the recordset itself.
FetchComplete has three Parameters:
- If adStatus is adStatusErrorsOccurred, then you can check pError to determine what error has occurred. This can happen if your code calls the Cancel method before the query is done executing, for example.
- The value of adStatus determines if any errors have occurred.
You may disable the FetchComplete event by setting the value of adStatus to the value adStatusUnwantedEvent. - pRecordset is a reference to the recordset itself.
Sample Code
The following sample demonstrates how to use the FetchProgress and FetchComplete events in Visual Basic.The sample uses an ODBC Datasource named Pubs to connect to the Pubs database that comes with SQL Server.
- In Microsoft Visual Basic, create a new Standard EXE. Form1 is added to the project by default.
- On the Project menu, click to select References, and then select Microsoft ActiveX Data Objects Library.
- On the Project menu, click to select Components, and then select Microsoft DataGrid Control 6.0 (OLEDB).
- Place a DataGrid, a Textbox, and a CommandButton onto Form1.
- Add the following code to Form1's Code Window:
Option Explicit Const strConn = "DSN=Pubs" Const strDefaultSQL = "SELECT * FROM Titles" Dim cn As ADODB.Connection Dim WithEvents rs As ADODB.Recordset Private Sub Form_Load() Command1.Caption = "Go" Text1.Text = strDefaultSQL Set cn = New ADODB.Connection cn.Open strConn End Sub Private Sub Command1_Click() Dim strSQL As String strSQL = Text1.Text Set rs = New ADODB.Recordset With rs .CursorLocation = adUseClient .Properties("Initial Fetch Size") = 2 .Properties("Background Fetch Size") = 4 Debug.Print "Start" Debug.Print "Initial Fetch Size: " & _ .Properties("Initial Fetch Size") Debug.Print "Background Fetch Size" & _ .Properties("Background Fetch Size") .Open strSQL, cn, , , adAsyncFetch End With End Sub Private Sub rs_FetchProgress(ByVal Progress As Long, _ ByVal MaxProgress As Long, _ adStatus As ADODB.EventStatusEnum, _ ByVal pRecordset As ADODB.Recordset) Debug.Print "Fetch: " & Progress & _ " Max: " & MaxProgress End Sub Private Sub rs_FetchComplete(ByVal pError As ADODB.Error, _ adStatus As ADODB.EventStatusEnum, _ ByVal pRecordset As ADODB.Recordset) If adStatus <> adStatusOK Then Debug.Print "Failed" Debug.Print "Error: " & pError.Number & " - " & pError.Description Else Set DataGrid1.DataSource = pRecordset Debug.Print "Done" End If End Sub
- Change strConn to a valid connection string for your database, and change strDefaultSQL to a valid SQL query that returns records from your database.
- Run the code. Click the Go button to start reading the Recordset. On the View menu, click to select Immediate Window. Visual Basic's Immediate window displays the progress of the asynchronous query.
For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
258904 (http://support.microsoft.com/kb/258904/EN-US/ ) BUG: FetchProgress Returns Incorrect Data with MDAC 2.1
224332 (http://support.microsoft.com/kb/224332/EN-US/ ) PRB: ADO Recordset Opened with adAsyncFetch May Seem Synchronous
- How To Use the ADO FetchProgress and FetchComplete Events
- 如何使用 ADO FetchProgress 和 FetchComplete 事件
- How to use Events in the Context of C#
- How to Create and Use the DLL
- How to use the CONVERSION_EXIT_PARVW_INPUT and CONVERSION_EXIT_PARVW_OUTPUT
- How to use the Erase Data and Disable Handheld command
- The robot API &&How to add and use sensor.
- How to register and use the native Kentico jQuery library
- How to use the CAknWaitDialog
- how to use the CAknwaitDialog
- How to use the CAknWaitDialog
- How to use the BAPI_BUPA_ADDRESS_CHANGE
- How to Access and Use the Windows Hosts File in Windows 7 and Vista
- How to use the UTF8Encoding class to encode and decode a text?
- How to install and use doxygen
- How To Use Function 'F4_FILENAME' and 'KD_GET_FILENAME_ON_F4'
- standard exception and how to use
- How to use 'SetItemData()' and 'GetItemData()'?
- Pattern-Oriented Software Architecture v1巨详细读书笔记 3
- 一个基于JUnit的测试框架
- Prototype 1.6 边学边译 -- Ajax Ajax.Updater
- oracle 数据库里查看表空间使用状况
- ADO Cancel 方法
- How To Use the ADO FetchProgress and FetchComplete Events
- 再说说SOA...
- 一剂良方克顽疾 英特尔I/O加速技术综述
- 【转】宁做创业狼,不做打工狗(ZT)
- Delphi 常用函数
- 程序员必备的职业素养
- 开博语录
- Browser Helper Objects: The Browser the Way You Want It
- 让程序响应组合键(c++builder&c#)