Download source files - 7 Kb
<!-- Article Starts -->
Because of multithreading support is available, the MFC ODBC classes no longer use
asynchronous processing. By default, drivers execute ODBC functions synchronously; that is,
the application calls a function and the driver does not return control to the application
until it has finished executing the function. However, some functions can be executed
asynchronously; that is, the application calls the function, and the driver, after minimal
processing, returns control to the application. The application can then call other functions
while the first function is still executing.
Asynchronous execution is supported for most functions that are largely executed on the
data source, such as the functions to prepare and execute SQL statements, retrieve metadata,
and fetch data. It is most useful when the task being executed on the data source takes a long
time, such as a complex query against a large database.
In MFC version 4.2 and higher,
CDatabase::SetSynchronousMode() function which
allows asynchronous execution has become obsolete. The MFC ODBC classes now use only synchronous
CAsyncRecordset provide support for
asynchronous execution. This allows an application perform other tasks while a complex query is
Usage is very simple. Copy CAsyncDatabase.* and CAsyncRecordset.* files into the project.
Do not forget
Examine the following samples:
- Open database as usual;
- Call CAsyncDatabase's
SQLStillExecuting() to determine whether the query is
- Call CDatabase's
Cancel() function with the HSTMT returned by
ExecuteSQLAsync() to cancel the query execution.
HSTMT hstmt = DB.ExecuteSQLAsync(strQuery); while(DB.SQLStillExecuting(hstmt))
- Open recordset using
OpenAsync(...). (parameters are the same as for
- Call CAsyncRecordset's
StillExecuting() to determine whether the query is still
- Call CRecordset's
Cancel() function to cancel the recordset opening (i.e. SELECT query execution).
rs.OpenAsync(CRecordset::snapshot, strQuery, CRecordset::executeDirect);
A Few Words About OnSetOptions() Virtual
The framework calls this member function to set initial options for the recordset or database.
OnSetOptions() determines the data
source's support for scrollable cursors and for cursor concurrency and sets the recordset's options
You can override OnSetOptions() to set additional options specific to the driver or the data source.
For example, if your data source supports opening for exclusive access, you might override
OnSetOptions() to take advantage of that ability.
CAsyncDatabase and CAsyncRecordset classes use OnSetOptions() to determine the data source's support
for asynchronous function execution and set options accordingly. (If data source does not support
asynchronous execution both
functions synchronously without warning user.) So when You override
OnSetOptions() be sure
Note:According to MFC documentation asynchronous processing slows performance.