INF: Using dbsqlsend() and dbdataready() in Windows

ID Number: Q73214

1.10

WINDOWS

Summary:

Because Windows is a cooperative multitasking environment, a Windows

DB-LIBRARY (dblib) program should be written to allow this

multitasking to take place. The dbsqlsend(), dbdataready() and

dbsqlok() dblib functions can be used to accomplish this task.

More Information:

Both dbsqlexec() and dbsqlsend() send a dblib query to the SQL Server

for processing. The dbsqlexec() function is a blocking function, and

maintains control of the CPU until the server completes its processing

and returns results. Windows cannot interrupt this; thus, all

multitasking is halted when using dbsqlexec().

However, the dbsqlsend() function is a nonblocking function that sends

the query and immediately returns control to the program. Then,

dbdataready() can be used to determine when the server has finished

processing, followed by dbsqlok() to verify the correctness of the

query.

Below is a code fragment for a DOS or OS/2 dblib program that uses

dbsqlsend() and dbdataready() to retain control of the program while

SQL Server is processing a query.

dbsqlsend (dbproc);

while ( dbdataready(dbproc) != TRUE )

{

printf ("Waiting for results...\n");

/* the program can do other work here... */

}

dbsqlok (dbproc);

A Windows dblib program can use three techniques:

1. The approach used above. This technique will allow the program to

continue working while the server is processing the query.

2. The PostMessage() Windows function. This technique is illustrated

below.

3. The Windows timer and the SetTimer() Windows function. This

technique is illustrated below.

The PostMessage() Windows Function

----------------------------------

Below is a code fragment illustrating the second technique using

PostMessage(). Windows can work on other applications after each

PostMessage(), and control will eventually return to the program.

Then, check dbdataready() after receiving each WM_CHECKQUERY message.

case WM_SENDQUERY:

dbsqlsend (dbproc);

PostMessage(hWnd,WM_CHECKQUERY,0,0L);

break;

case WM_CHECKQUERY:

if ( dbdataready(dbproc) )

{

dbsqlok (dbproc);

PostMessage(hWnd,WM_GETRESULTS,0,0L);

}

else

{

PostMessage(hWnd,WM_CHECKQUERY,0,0L);

}

break;

The Windows Timer and the SetTimer() Windows Function

-----------------------------------------------------

Below is a code fragment illustrating the third technique. The

SetTimer() function creates a timer that sends a WM_TIMER message to

your application every 1000 milliseconds. Then, check dbdataready()

after each WM_TIMER message.

case WM_SENDQUERY:

dbsqlsend (dbproc);

SetTimer (hWnd, 1, 1000, 0);

break;

case WM_TIMER:

if ( dbdataready(dbproc) )

{

dbsqlok (dbproc);

KillTimer (hWnd, 1);

PostMessage(hWnd,WM_GETRESULTS,0,0L);

}

break;