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

  1. /*    example3.c */
  2. /*
  3. ** This example selects some information from the "pubs" database.
  4. ** It illustrates binding of both aggregate and compute results.
  5. **
  6. ** Note that this example only work if the "pubs" database exists
  7. ** on your SQL Server.
  8. */
  9.  
  10. #if defined(DBNTWIN32)
  11. #include <windows.h>
  12. #endif
  13.  
  14. #include <stdio.h>
  15. #include <sqlfront.h>
  16. #include <sqldb.h>
  17.  
  18. #define PLEN 4
  19. #define DATEPRINT 26
  20. #define MONEYPRINT 12
  21.  
  22. /* Forward declarations of the error handler and message handler.
  23. */
  24. int    err_handler();
  25. int    msg_handler();
  26.  
  27. main(argc,argv)
  28. int    argc;
  29. char    *argv[];
  30. {
  31.     LOGINREC    *login;
  32.     DBPROCESS    *dbproc;
  33.  
  34.     /* Declare the datatypes for the columns in the table "titles". */
  35.     DBINT    pcount;
  36.     DBINT    sales;
  37.     DBINT    salesavg;
  38.     DBINT    sumsale;
  39.     DBCHAR    date[DATEPRINT+1];
  40.     DBCHAR    price[MONEYPRINT+1];
  41.     DBCHAR    priceavg[MONEYPRINT+1];
  42.     DBCHAR    pubid[PLEN+1];
  43.     RETCODE    result_code; /* to hold the results of dbresults(). */
  44.     STATUS    row_code;    /* to hold the results of dbnextrow(). */
  45.  
  46.         dbinit();        /* initialize dblib */
  47.  
  48.  
  49.     /* Install the user-supplied error-handling and message-handling
  50.     * functions. They are defined at the bottom of this source file.
  51.     */
  52.     dbmsghandle((DBMSGHANDLE_PROC)msg_handler);
  53.     dberrhandle((DBERRHANDLE_PROC)err_handler);
  54.  
  55.     /* Set up the login information. */
  56.  
  57.     login = dblogin();
  58.     DBSETLUSER(login, "user");
  59.     DBSETLPWD(login, "my_passwd");
  60.     DBSETLAPP(login, "example3");
  61.     DBSETLVERSION(login, DBVER60);
  62.  
  63.     dbproc = dbopen(login, "my_server");
  64.  
  65.     /* Send a "use database" command. */
  66.     dbuse(dbproc,"pubs");
  67.  
  68.     /* Put the SQL statement into the command buffer. */
  69.     dbcmd(dbproc, "select pub_id, pubdate, price, avg(price), ytd_sales,");
  70.     dbcmd(dbproc, " avg(ytd_sales), sum(ytd_sales) from titles");
  71.         dbcmd(dbproc, " group by pub_id, pubdate, price, ytd_sales");
  72.     dbcmd(dbproc, " order by pub_id");
  73.     dbcmd(dbproc, " compute count(pub_id) by pub_id");
  74.  
  75.     /* Send the command buffer to SQL Server for execution. */
  76.     dbsqlexec(dbproc);
  77.  
  78.     /*
  79.     ** Using the aggregates "sum" and "avg" with the COMPUTE clause
  80.     ** necessitates special handling when binding the results. Since each
  81.     ** aggregate creates a new column, this is accounted for in the bind.
  82.     ** Notice that avg(price) is the fourth column in the select list
  83.     ** and is also specified as the fourth column in the dbbind() function.
  84.     **
  85.     ** The COMPUTE clause creates a compute row, which requires a
  86.     ** special bind function called dbaltbind().
  87.     */
  88.  
  89.     while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS)
  90.     {
  91.         if (result_code == SUCCEED)
  92.         {
  93.             dbbind(dbproc,1,NTBSTRINGBIND, (DBINT) 0,pubid);
  94.             dbbind(dbproc,2,NTBSTRINGBIND, (DBINT) 0,date);
  95.             dbbind(dbproc,3,NTBSTRINGBIND, (DBINT) 0,price);
  96.             dbbind(dbproc,4,NTBSTRINGBIND, (DBINT) 0,priceavg);
  97.             dbbind(dbproc,5,INTBIND, (DBINT) 0, (BYTE *) &sales);
  98.             dbbind(dbproc,6,INTBIND, (DBINT) 0, (BYTE *) &salesavg);
  99.             dbbind(dbproc,7,INTBIND, (DBINT) 0, (BYTE *) &sumsale);
  100.  
  101.             /* dbaltbind() binds compute columns. */
  102.             dbaltbind(dbproc,1,1,INTBIND,(DBINT) 0,(BYTE *)&pcount);
  103.  
  104.             printf("\nAccounts:\n");
  105.             printf("---------\n\n");
  106.             printf
  107.             ("%-5s    %-19s    %-6s    %-10s    %-5s    %-10s    %-10s\n\n",
  108.             "pubid", "date", "price", "avg(price)",
  109.             "sales", "avg(sales)", "sum(sales)");
  110.  
  111.             /*
  112.             ** Print out each result row, using different statements
  113.             ** depending on whether the row is a regular row or a
  114.             ** compute row.
  115.             */
  116.  
  117.             while ((row_code = dbnextrow(dbproc)) != NO_MORE_ROWS)
  118.             {
  119.                 if (row_code == REG_ROW)
  120.                 {
  121.                     printf
  122.                 ("%5s    %19s    %6s    %10s    %5ld    %10ld    %10ld\n",
  123.                 pubid, date, price, priceavg, sales,
  124.                 salesavg, sumsale);
  125.                 }
  126.                 else
  127.                 printf("title count:    %ld\n\n",pcount);
  128.             }
  129.         }
  130.     }
  131.  
  132.     dbexit();
  133.     return(STDEXIT);
  134. }
  135.  
  136. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  137. DBPROCESS    *dbproc;
  138. int    severity;
  139. int    dberr;
  140. int    oserr;
  141. char    *dberrstr;
  142. char    *oserrstr;
  143. {
  144.     printf("DB-LIBRARY error:\n\t%s\n", dberrstr);
  145.  
  146.     if (oserr != DBNOERR)
  147.         printf("Operating-system error:\n\t%s\n", oserrstr);
  148.  
  149.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  150.         return(INT_EXIT);
  151.  
  152.     return(INT_CANCEL);
  153. }
  154.  
  155. int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
  156. DBPROCESS    *dbproc;
  157. DBINT    msgno;
  158. int    msgstate;
  159. int    severity;
  160. char    *msgtext;
  161. {
  162.     printf
  163.     ("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  164.     msgno, msgstate, severity, msgtext);
  165.     return(0);
  166. }
  167.  
  168.  
  169.