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 / example4 / example4.c next >
Encoding:
C/C++ Source or Header  |  1996-04-03  |  4.5 KB  |  198 lines

  1. /*    example4.c */
  2. /*
  3. ** This example accesses the data within each row without using dbbind()
  4. ** and illustrates the use of row buffering.
  5. **
  6. ** It runs a query, saves all of the returned rows (up to a maximum
  7. ** of 100) using DB-LIBRARY row buffering, and allows the user to
  8. ** examine data rows at random.
  9. */
  10.  
  11. #if defined(DBNTWIN32)
  12. #include <windows.h>
  13. #endif
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <sqlfront.h>
  18. #include <sqldb.h>
  19.  
  20. DBPROCESS    *dbproc;    /* Our connection with SQL Server. */
  21. LOGINREC    *login;    /* Our login information. */
  22.  
  23. #define TYPELEN 2
  24. #define DATELEN 25
  25.  
  26. /* Forward declarations of the error handler and message handler.
  27. */
  28. int err_handler(DBPROCESS*, int, int, int, char*, char*);
  29. int msg_handler(DBPROCESS*, DBINT, int, int, char*);
  30.  
  31. main(argc, argv)
  32. int    argc;
  33. char    *argv[];
  34. {
  35.     /* Here are the variables which will be used to store the
  36.     * data being examined.
  37.     */
  38.     DBCHAR    name[MAXNAME+1];    /* MAXNAME is defined in
  39.     * "sqldb.h" as the maximum
  40.     * length for names of database
  41.     * objects, such as tables,
  42.     * columns, and procedures.
  43.     */
  44.     DBCHAR    type[TYPELEN+1];
  45.     DBINT    id;
  46.  
  47.     DBCHAR    datebuf[64];
  48.     DBINT    len;
  49.     char    numstring[32];
  50.     int    quitflag = 0;
  51.     RETCODE    row_code;
  52.     DBINT    rownum;
  53.  
  54.         dbinit();        /* initialize dblib */
  55.  
  56.     /* Install the user-supplied error-handling and message-handling
  57.     * functions. They are defined at the bottom of this source file.
  58.     */
  59.     dbmsghandle((DBMSGHANDLE_PROC)msg_handler);
  60.     dberrhandle((DBERRHANDLE_PROC)err_handler);
  61.  
  62.     /*
  63.     ** Get a LOGINREC structure and fill it with the necessary
  64.     ** login information.
  65.     */
  66.  
  67.     login = dblogin();
  68.     DBSETLUSER (login, "user");
  69.     DBSETLPWD(login, "my_passwd");
  70.     DBSETLAPP(login, "example4");
  71.     DBSETLVERSION(login, DBVER60);
  72.  
  73.     dbproc = dbopen(login, "my_server");
  74.     dbcmd(dbproc, "select name, type, id, crdate from sysobjects");
  75.     dbcmd(dbproc, " where type = 'S'");
  76.  
  77.     /*
  78.         ** Set row buffering to 100 rows, via dbsetopt().
  79.     ** Note that this parameter must be passed as an ASCII string.
  80.     */
  81.  
  82.     dbsetopt(dbproc, DBBUFFER, "100");
  83.     dbsqlexec(dbproc);
  84.     if (dbresults(dbproc) == SUCCEED)
  85.     {
  86.         /* Read all of the rows into DB-LIBRARY's buffer */
  87.         while ((row_code = dbnextrow(dbproc)) != NO_MORE_ROWS)
  88.         {
  89.             /* If DB-LIBRARY's row buffer is full, throw
  90.             * away the oldest row to allow the newest
  91.             * row to be read in.
  92.             */
  93.             if (row_code == BUF_FULL)
  94.                 dbclrbuf(dbproc, (DBINT) 1);
  95.         }
  96.  
  97.         /* Print out the column headers. */
  98.  
  99.         puts("NAME                 TYPE DATE                   ID");
  100.         puts("-------------------- ---- ---------------------- ----");
  101.  
  102.  
  103.         /* Let the user view any row in the table. */
  104.  
  105.         printf("Type the number of the row you want to see.\n");
  106.         printf("The first row is number 1.\n");
  107.         printf("Asking for row 0 will terminate the program.\n");
  108.  
  109.         while (quitflag == 0)
  110.         {
  111.             printf("Row number: ");
  112.             gets(numstring);
  113.             rownum = atoi(numstring);
  114.  
  115.         if (rownum == 0)
  116.             quitflag = 1;
  117.         else
  118.         {
  119.             /* Print out the requested row. */
  120.             if (dbgetrow(dbproc, rownum) == NO_MORE_ROWS)
  121.                 printf
  122.                 ("That row is not in the table.\n");
  123.             else
  124.             {
  125.                 /* Copy variable-length character data
  126.                 * (colname).
  127.                     */
  128.  
  129.                 strncpy
  130.                 (name, (DBCHAR *)dbdata(dbproc, 1),
  131.                 (len = dbdatlen(dbproc, 1)));
  132.  
  133.                 /* String needs terminating null. */
  134.  
  135.                 name[len] = '\0';
  136.  
  137.                 /* Copy fixed-length character data. */
  138.  
  139.                 strncpy
  140.                 (type, (DBCHAR *)dbdata(dbproc, 2),
  141.                 (len = dbdatlen(dbproc, 2)));
  142.                 type[len] = '\0';
  143.  
  144.                 /* Copy integer data. */
  145.                 id = *((DBINT *)dbdata(dbproc, 3));
  146.  
  147.                 /* Convert datetime data to a printable
  148.                 * string.
  149.                 */
  150.  
  151.                 dbconvert(dbproc, SQLDATETIME, (dbdata(dbproc, 4)),
  152.                 (DBINT)-1, SQLCHAR, datebuf, (DBINT)-1);
  153.  
  154.  
  155.                 printf("%-20s %-4s %-22s %-4ld \n",
  156.                 name, type, datebuf, id);
  157.                 }
  158.             }
  159.         }
  160.     }
  161.  
  162.     dbexit();
  163.     return(STDEXIT);
  164. }
  165.  
  166. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  167. DBPROCESS    *dbproc;
  168. int    severity;
  169. int    dberr;
  170. int    oserr;
  171. char    *dberrstr;
  172. char    *oserrstr;
  173. {
  174.     printf("DB-LIBRARY error:\n\t%s\n", dberrstr);
  175.  
  176.     if (oserr != DBNOERR)
  177.         printf("Operating-system error:\n\t%s\n", oserrstr);
  178.  
  179.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  180.         return(INT_EXIT);
  181.  
  182.     return(INT_CANCEL);
  183. }
  184.  
  185. int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
  186. DBPROCESS    *dbproc;
  187. DBINT    msgno;
  188. int    msgstate;
  189. int    severity;
  190. char    *msgtext;
  191. {
  192.     printf
  193.     ("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  194.     msgno, msgstate, severity, msgtext);
  195.     return(0);
  196. }
  197.  
  198.