int err_handler (PDBPROCESS dbproc, INT severity,
INT dberr, INT oserr, LPCSTR dberrstr, LPCSTR oserrstr)
printf ("DB-Library Error %i: %s\n", dberr, dberrstr);
if (oserr != DBNOERR)
printf ("Operating System Error %i: %s\n", oserr, oserrstr);
int msg_handler (PDBPROCESS dbproc, DBINT msgno, INT msgstate,
INT severity, LPCSTR msgtext, LPCSTR server,
LPCSTR procedure, DBUSMALLINT line)
printf ("SQL Server Message %ld: %s\n", msgno, msgtext);
This example illustrates features common to most DB-Library for C applications, including:
All source files that contain calls to DB-Library functions require two header files, Sqlfront.h and Sqldb.h.
Before including the Sqlfront.h and Sqldb.h files, define the target operating system with #define:
DBMSDOS (for Microsoft MS-DOS?)
DBMSWIN (for 16-bit Microsoft Windows?)
DBNTWIN32 (for 32-bit Windows 95 and Microsoft Windows NT?)
An alternative is to put DBMSDOS, DBMSWIN, or DBNTWIN32 on the compilation command lines. For more information, see the examples in “Include Files,” in Building Applications.
For Windows, Windows 95, and Windows NT, you must include Windows.h before including the Sqlfront.h and Sqldb.h files.
Include Sqlfront.h before Sqldb.h. Sqlfront.h defines symbolic constants, such as function return values and the exit values STDEXIT and ERREXIT. These exit values can be used as the parameter for the C standard library function exit. The exit values are defined appropriately for the operating system running the program. The Sqlfront.h file also includes type definitions for data types that can be used in program variable declarations. These data types are described in DB-Library for C Data types.
The Sqldb.h file contains additional type definitions and DB-Library function prototypes, most of which are meant to be used only by the DB-Library functions. They should not be accessed directly by the program. To ensure compatibility with future releases of DB-Library, use the contents of Sqldb.h only as documented here.
dberrhandle and dbmsghandle
The first of these DB-Library functions, dberrhandle, installs a user-supplied error-handling function, which is called automatically whenever the application encounters a DB-Library error. Similarly, dbmsghandle installs a message-handling function, which is called in response to informational or error messages returned from SQL Server. The error- and message-handling functions are user-supplied. It is strongly recommended that users supply error-processing functions.
Supplies a LOGINREC structure, which DB-Library uses to log in to SQL Server. Two functions set entries in the LOGINREC. DBSETLPWD sets the password that DB-Library uses when logging in. DBSETLAPP sets the name of the application, which appears in the SQL Server sysprocess table. Certain functions set other aspects of the LOGINREC, which contains defaults for each value they set.
Opens a connection between the application and SQL Server. It uses the LOGINREC supplied by dblogin to log in to the server. It returns a DBPROCESS structure, which serves as the conduit for information between the application and the server. After this function has been called, the application is connected with SQL Server and can send Transact-SQL statements to SQL Server and process the results. Simultaneous transactions must each have a distinct DBPROCESS. Serial transactions can use the same DBPROCESS.
Fills the command buffer with Transact-SQL statements, which can then be sent to SQL Server. Each call to dbcmd, after the first, adds the supplied text to the end of any text already in the buffer. The programmer must supply necessary blanks between words, such as the space between the quotation mark and the word WHERE in the second dbcmd call in the example:
dbcmd(dbproc, " WHERE state = 'CA' ");
Although multiple statements can be included in the buffer, this example only shows how to send and process a single statement. DB-Library allows an application to send multiple statements (called a command batch) to SQL Server and process each statement’s set of results separately.
Executes the command buffer; that is, it sends the contents of the buffer to SQL Server, which parses and executes the commands. This function causes DB-Library to wait until SQL Server has completed execution of the query. To avoid this delay, you can call dbsettime to set the DB-Library time-out, or you can use dbsqlsend, dbdataready, and dbsqlok (instead of dbsqlexec) to retain control while SQL Server is busy.
Gets the results of the current Transact-SQL statement ready for processing. After dbresults returns SUCCEED, column metadata for the current result set is available. Your program should call dbresults until it returns NO_MORE_RESULTS. If your program fails to do this, the DB-Library error message 10038 “Results Pending” occurs the next time that DBPROCESS is used.
Binds result columns to program variables. In the example, the first call to dbbind binds the first result column to the name variable. In other words, when the program reads a result row by calling dbnextrow, the contents of the first column in the result row are placed in the name variable. The data type of the binding is NTBSTRINGBIND, one of several binding types available for character data. The second call binds the second result column to the city variable.
Reads a row and places the results in the program variables specified by the earlier dbbind calls. Each successive call to dbnextrow reads another result row until the last row has been read and NO_MORE_ROWS is returned. Processing of the results must take place inside the dbnextrow loop. This is because each call to dbnextrow overwrites the previous values in the bound program variables.
Closes all SQL Server connections and frees all DBPROCESS structures created because of the application. It is usually the last DB-Library function in the program.
(c) 1988-98 Microsoft Corporation. All Rights Reserved.