home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / sql / dblib / c / sqltestc / sqltestc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-03  |  4.5 KB  |  149 lines

  1. /*************************************************************************
  2.  
  3. SQLTESTC - SQL Data Server sample program for console-based Windows NT.
  4.          
  5. Copyright (c), 1995 by Microsoft Corp.
  6.  
  7. *************************************************************************/
  8.  
  9. #define    DBNTWIN32        // must identify operating system environment
  10. #include "windows.h"
  11.  
  12. #include <sqlfront.h>
  13. #include <sqldb.h>       // DB-LIB header file (should always be included)
  14. #include <stdio.h>
  15.  
  16.  
  17. main ()
  18. {
  19.     PDBPROCESS dbproc;  // allocate    a DB-LIB process structure
  20.     PLOGINREC  login;   // allocate    a DB-LIB login structure
  21.  
  22.     // Variables used to store the returning data
  23.     char       au_lname[41];
  24.     char       au_fname[20];
  25.     char       id[12];
  26.     char       phone[13];
  27.     char       address[41];
  28.     char       city[21];
  29.     char       state[3];
  30.     char       zip[6];
  31.     char       getname[41];
  32.     char       Servername[25];
  33.     RETCODE    result_code;
  34.  
  35.     // Forward declarations of the error handler and message handler.
  36.     int err_handler(PDBPROCESS, int, int, int, char*, char*);
  37.     int msg_handler(PDBPROCESS, DBINT, int, int, char*);
  38.  
  39.     if (dbinit() == (char *)NULL)
  40.     {
  41.         printf("Communications layer not loaded\n");
  42.         return(1);
  43.     }
  44.     
  45.     // Install the user-supplied error-handling    and message-handling
  46.     // routines. They are defined at the bottom    of this    source file.
  47.     
  48.     dberrhandle((DBERRHANDLE_PROC)err_handler);
  49.     dbmsghandle((DBMSGHANDLE_PROC)msg_handler);
  50.  
  51.     // Get server's computer name
  52.     Servername[0] = '\0';
  53.     printf ("\nEnter Name of SQL Server: ");
  54.     gets (Servername);
  55.  
  56.     login = dblogin();                     // get login record from DB-LIB
  57.     DBSETLUSER (login, (char *)"sa");      // set the username
  58.     DBSETLAPP (login, (char *)"sqltestp"); // set the application name
  59.     DBSETLPWD (login, (char *)"");         // set the SQL Server password
  60.     DBSETLVERSION(login,DBVER60);
  61.  
  62.     // Now attempt to create and initialize a DBPROCESS structure
  63.     if ((dbproc    = dbopen (login, Servername)) == NULL)
  64.     {
  65.         printf ("dbopen failed\n");
  66.         return (1); // exit program
  67.     }
  68.  
  69.     dbuse (dbproc, "pubs"); // use the "pubs" database
  70.  
  71.     while (TRUE)
  72.     {
  73.         printf ("\nEnter author's last name to retrieve (return to exit): ");
  74.         gets (getname);
  75.  
  76.         if (getname[0] == '\0') // if only a return was entered
  77.             break;
  78.  
  79.         // construct command buffer to be sent to the SQL server
  80.         dbcmd (dbproc, (char *)"select au_id, au_lname, au_fname, phone,");
  81.         dbcmd (dbproc, (char *)" address, city, state, zip");
  82.         dbcmd (dbproc, (char *)" from authors");
  83.         dbcmd (dbproc,    (char *)" where au_lname = '");
  84.         dbcmd (dbproc, (char *)getname);
  85.         dbcmd (dbproc, (char *)"'");
  86.  
  87.         dbsqlexec (dbproc); // send command buffer to SQL server
  88.  
  89.         // now check the results from the SQL server
  90.         while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS)
  91.         {
  92.             if (result_code == SUCCEED)
  93.             {
  94.                 dbbind (dbproc,    1, NTBSTRINGBIND, (DBINT) 0, (char *)id);
  95.                 dbbind (dbproc,    2, NTBSTRINGBIND, (DBINT) 0, (char *)au_lname);
  96.                 dbbind (dbproc,    3, NTBSTRINGBIND, (DBINT) 0, (char *)au_fname);
  97.                 dbbind (dbproc,    4, NTBSTRINGBIND, (DBINT) 0, (char *)phone);
  98.                 dbbind (dbproc,    5, NTBSTRINGBIND, (DBINT) 0, (char *)address);
  99.                 dbbind (dbproc,    6, NTBSTRINGBIND, (DBINT) 0, (char *)city);
  100.                 dbbind (dbproc,    7, NTBSTRINGBIND, (DBINT) 0, (char *)state);
  101.                 dbbind (dbproc,    8, NTBSTRINGBIND, (DBINT) 0, (char *)zip);
  102.  
  103.                 // now process the rows
  104.                 while (dbnextrow(dbproc) != NO_MORE_ROWS)
  105.                 {
  106.                     printf ("Author ID:  %s\n",    id);
  107.                     printf ("Last Name:  %s\n",    au_lname);
  108.                     printf ("First Name: %s\n",    au_fname);
  109.                     printf ("Address:    %s\n",    address);
  110.                     printf ("City:       %s\n",    city);
  111.                     printf ("State:      %s\n",    state);
  112.                     printf ("Zip Code:   %s\n",    zip);
  113.                     printf ("Telephone:  %s\n",    phone);
  114.                     printf ("\n");
  115.                 }
  116.             }
  117.             else
  118.             {
  119.                 printf ("Results Failed\n");
  120.                 break;
  121.             }
  122.         }
  123.     } // while (TRUE)
  124.  
  125.     // Close the connection and exit
  126.     dbexit();
  127. }
  128.  
  129. int err_handler(PDBPROCESS dbproc, int severity, int dberr, int oserr, char * dberrstr, char * oserrstr)
  130. {
  131.     if (dberrstr != NULL)
  132.         printf("DB-LIBRARY error:\n\t%s\n", dberrstr);
  133.  
  134.     if (oserr != DBNOERR)
  135.         printf("Operating-system error:\n\t%s\n", oserrstr);
  136.  
  137.     if ((dbproc == NULL) ||    (DBDEAD(dbproc)))
  138.         return(INT_EXIT);
  139.     else
  140.         return(INT_CANCEL);
  141. }
  142.  
  143. int msg_handler(PDBPROCESS dbproc, DBINT msgno, int msgstate, int severity, char * msgtext)
  144. {
  145.     printf("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  146.             msgno, msgstate, severity, msgtext);
  147.     return(0);
  148. }
  149.