Click here to Skip to main content
13,140,224 members (49,000 online)
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 8 Dec 1999

Asynchronous Query Execution

, 8 Dec 1999
Rate this:
Please Sign up or sign in to vote.
Two classes that provide support for asynchronous SQL execution.
  • 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 processing.

    Two classes CAsyncDatabase and CAsyncRecordset provide support for asynchronous execution. This allows an application perform other tasks while a complex query is being executed.

    Usage is very simple. Copy CAsyncDatabase.* and CAsyncRecordset.* files into the project. Do not forget

    #include "AsyncDatabase.h"
    #include "AsyncRecordset.h"

    Examine the following samples:

    For CAsyncDatabase:

    • Open database as usual;
    • Call ExecuteSQLAsync(<sql_query_text_here>);
    • Call CAsyncDatabase's SQLStillExecuting() to determine whether the query is still executing;
    • Call CDatabase's Cancel() function with the HSTMT returned by ExecuteSQLAsync() to cancel the query execution.

    CAsyncDatabase Sample

    // asynchronous query execution
    void ExecuteLongQuery(strQuery)
    	CAsyncDatabase DB;
    	HSTMT hstmt = DB.ExecuteSQLAsync(strQuery);	// complex query
    		// check for Cancel command, for exmpl
    		if(Cancelled()) 	// by Cancel button, for example

    For CAsyncRecordset:

    • Open recordset using OpenAsync(...). (parameters are the same as for Open());
    • Call CAsyncRecordset's StillExecuting() to determine whether the query is still executing;
    • Call CRecordset's Cancel() function to cancel the recordset opening (i.e. SELECT query execution).

    CAsyncRecordset Sample

    // Open Asynchronous Recordset
    	// DB is a CDatabase
    	CAsyncRecordset rs(&DB);
    	rs.OpenAsync(CRecordset::snapshot, strQuery, CRecordset::executeDirect);
    		ContinueTask();	// perform some task

    A Few Words About OnSetOptions() Virtual

    The framework calls this member function to set initial options for the recordset or database. CRecordset and CDatabase's OnSetOptions() determines the data source's support for scrollable cursors and for cursor concurrency and sets the recordset's options accordingly.

    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 CAsyncDatabase and CAsyncRecordset execute functions synchronously without warning user.) So when You override OnSetOptions() be sure you call CAsyncDatabase::OnSetOptions() or CAsyncRecordset::OnSetOptions() from derived class OnSetOptions().

    Note:According to MFC documentation asynchronous processing slows performance.


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Vachik Hovhannisyan
    United States United States
    No Biography provided

    You may also be interested in...

    Comments and Discussions

    GeneralAsynchronous Query execution in .Net Pin
    elizas8-Feb-10 2:43
    groupelizas8-Feb-10 2:43 
    Sometimes, we have requirements in which we have to retrieve data from different tables at one time that take too long time to execute because of bulk data. In that case we have to wait for execution of first query to complete to start the second one. This makes a long waiting time and hence poor performance.
    With ADO.Net 2.0, there is a new feature introduced for executing commands asynchronously. In ADO.NET 1.1 and prior versions, we have to wait for one query to finish the execution, then to execute another query. But now we can execute multiple queries asynchronously without waiting.To implement this we have to provide an additional setting in the connection string and that is to set “Asynchronous Processing=true”. In case you have a requirement to use both synchronous and asynchronous operation then use two separate connection strings, one with “Asynchronous Processing=true” and other with “Asynchronous Processing=false” for better performance.


    GeneralDynamicRecordset.h Pin
    Do Manh Hung1-Apr-03 17:19
    memberDo Manh Hung1-Apr-03 17:19 
    GeneralRe: DynamicRecordset.h Pin
    Rolando E. Cruz-Marshall23-Dec-08 8:38
    memberRolando E. Cruz-Marshall23-Dec-08 8:38 
    GeneralAsynchronous query execution vs SQLCancel Pin
    Tom Tempelaere, aka TiTi3-Sep-01 2:00
    memberTom Tempelaere, aka TiTi3-Sep-01 2:00 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    Permalink | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.170915.1 | Last Updated 9 Dec 1999
    Article Copyright 1999 by Vachik Hovhannisyan
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid