DB-Library/C programming

An application programmer writes a DB-Library program, using calls to DB-Library routines to set up DB-Library structures, connect to servers, send commands, process results, and clean up. A DB-Library program is compiled and run in the same way as any other C language program.

Programming with DB-Library/C typically involves a few basic steps:

  1. Logging into a server.

  2. Placing language commands into a buffer and sending them to the server.

  3. Processing the results, if any, returned from the server, one command at a time and one result row at a time. The results can be placed in program variables, where they can be manipulated by the application.

  4. Handling DB-Library/C errors and server messages.

  5. Closing the connection with the server.

The example below shows the basic framework of many DB-Library/C applications. The program opens a connection to a Adaptive Server, sends a Transact-SQL select command to the server, and processes the set of rows resulting from the select. Note that this program does not include the error or message handling routines; those routines are illustrated in the sample programs included with DB-Library.

             #include <sybfront.h> 
             #include <sybdb.h> 
             #include <syberror.h> 
 
             /* Forward declarations of the error handler and message
             ** handler. 
             */ 
             interr_handler(); 
             intmsg_handler(); 
 
             main() 
             { 
                  DBPROCESS    *dbproc;     /* The connection with */
                                            /* Adaptive Server */ 
                  LOGINREC     *login;      /* The login information */
                  DBCHAR       name[40];
                  DBCHAR       city[20];
                  RETCODE      return_code;
 
                  /* Initialize DB-Library */ 
                  if (dbinit() == FAIL) 
                  exit(ERREXIT);
 
                  /*  
                  ** Install user-supplied error-handling and message- 
                  ** handling routines. The code for these is omitted 
                  ** from this example for conciseness. 
                  */ 
                  dberrhandle(err_handler); 
                  dbmsghandle(msg_handler);
 
                  /* Get a LOGINREC */ 
                  login = dblogin(); 
                  DBSETLPWD(login, "server_password"); 
                  DBSETLAPP(login, "example"); 
 
                  /* Get a DBPROCESS structure for communication */
                  /* with Adaptive Server. */ 
                  dbproc = dbopen(login, NULL); 
 
                  /* 
                  ** Retrieve some columns from the "authors" table 
                  ** in the "pubs2" database. 
                  */ 
 
                  /* First, put the command into the command buffer. */ 
                  dbcmd(dbproc, "select au_lname, city from
                        pubs2..authors");   
                dbcmd(dbproc, "
                     where state = ’CA’ "); 
 
                  /* 
                  ** Send the command to Adaptive Server and start execution
                  */ 
                  dbsqlexec(dbproc);
 
                  /* Process the command */ 
                  while ((return_code = dbresults(dbproc)) !=
                       NO_MORE_RESULTS) 
                  { 
                       if (return_code == SUCCEED) 
                  { 
                       /* Bind results to program variables. */ 
                       dbbind(dbproc, 1, STRINGBIND, (DBINT)0, name); 
                       dbbind(dbproc, 2, STRINGBIND, (DBINT)0, city);
 
                       /* Retrieve and print the result rows. */ 
                       while (dbnextrow(dbproc) != NO_MORE_ROWS) 
                       { 
                       printf ("%s: %s\n", name, city); 
                       } 
                   } 
                } 
 
                /* Close the connection to Adaptive Server */ 
                dbexit(); 
             } 

The example illustrates features common to most DB-Library/C applications:

Although DB-Library/C contains a great number of routines, much can be accomplished with just the few routines shown in this example.