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

  1. /*    example1.c */
  2. /*
  3. **    This example illustrates how to send two queries to
  4. **    SQL Server in a command batch.    It binds each set
  5. **    of results and prints the rows.
  6. **
  7. */
  8.  
  9. #if defined(DBNTWIN32)
  10. #include <windows.h>
  11. #endif
  12.  
  13. #include <stdio.h>
  14. #include <sqlfront.h>
  15. #include <sqldb.h>
  16.  
  17. #define DATELEN    26
  18. #define TYPELEN    2
  19.  
  20. /* Forward declarations of the error handler and message handler.
  21. */
  22. int err_handler(DBPROCESS*, int, int, int, char*, char*);
  23. int msg_handler(DBPROCESS*, DBINT, int, int, char*);
  24.  
  25. main(argc, argv)
  26. int    argc;
  27. char    *argv[];
  28. {
  29.     DBPROCESS    *dbproc;    /* Our connection with SQL Server. */
  30.     LOGINREC    *login;    /* Our login information. */
  31.  
  32.     /* These are the variables used to store the returning data. */
  33.  
  34.     DBCHAR    crdate[DATELEN+1];
  35.     DBINT    id;
  36.     DBCHAR    name[MAXNAME+1];    /* MAXNAME is defined in
  37.     * "sqldb.h" as the maximum
  38.     * length for names of database
  39.     * objects, such as tables,
  40.     * columns, and procedures.
  41.     */
  42.     DBCHAR    type[TYPELEN+1];
  43.     RETCODE    result_code;
  44.  
  45.         dbinit();        /* initialize dblib */
  46.  
  47.     /* Install the user-supplied error-handling and message-handling
  48.     * functions. They are defined at the bottom of this source file.
  49.     */
  50.     dbmsghandle((DBMSGHANDLE_PROC)msg_handler);
  51.     dberrhandle((DBERRHANDLE_PROC)err_handler);
  52.  
  53.     /*
  54.     ** Get a LOGINREC structure and fill it with the necessary
  55.     ** login information.
  56.     */
  57.  
  58.     login = dblogin();
  59.     DBSETLUSER(login, "user");
  60.     DBSETLPWD(login, "my_passwd");
  61.     DBSETLAPP(login, "example1");
  62.     DBSETLVERSION(login, DBVER60);
  63.  
  64.     /*
  65.     ** Get a DBPROCESS structure for communicating with SQL Server.
  66.     */
  67.  
  68.     dbproc = dbopen(login, "my_server");
  69.  
  70.     /*
  71.     ** We are going to retrieve some information from a table
  72.     ** named "sysobjects", regarding names of system tables and
  73.     ** stored procedures.
  74.     ** We will submit two queries.    The first finds all the rows
  75.     ** that describe system tables.    The second finds all the rows
  76.     ** that describe stored procedures.    The program will only look
  77.     ** at the first 10 rows that describe stored procedures.
  78.     */
  79.  
  80.     /* First, put the commands into the command buffer. */
  81.  
  82.     dbcmd(dbproc, "select name, type, id, crdate from sysobjects");
  83.     dbcmd(dbproc, " where type = 'S' ");
  84.     dbcmd(dbproc, "select name, type, id, crdate from sysobjects");
  85.     dbcmd(dbproc, " where type = 'P' ");
  86.  
  87.     /*
  88.     ** SQL Server processes the command batch in the following
  89.     ** order:
  90.     **
  91.     ** 1)    It checks for syntax errors (i.e., "use database pubs"
  92.     **        is syntactically incorrect; it should be "use pubs").
  93.     ** 2)    The second check is a semantic check (i.e., "select * from
  94.     **        titels" is incorrect because the spelling should be
  95.     **        "titles".)
  96.     ** 3) The third check occurs in the actual execution phase. This
  97.     **    check involves issues like permissions or memory problems.
  98.     **
  99.     ** In the execution phase, dbsqlexec() and dbresults() can return
  100.     ** the value "SUCCEED", which means there are more commands in the
  101.     ** batch to process and that command was successful. A value
  102.     ** of "FAIL" means that the query failed but there may be more
  103.     ** commands in the batch to process. A value of "NO_MORE_RESULTS"
  104.     ** means that there are no more commands in the batch to process.
  105.     ** Therefore, the programmer must check the return values after
  106.     ** dbsqlexec() and dbresults(), as illustrated below.
  107.     **
  108.     */
  109.  
  110.     /* Send the commands to SQL Server and start execution. */
  111.     dbsqlexec(dbproc);
  112.  
  113.     /* Process each command until there are no more. */
  114.  
  115.     while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS)
  116.     {
  117.         if (result_code == SUCCEED)
  118.         {
  119.             /* Bind program variables. */
  120.  
  121.             dbbind(dbproc, 1, NTBSTRINGBIND, (DBINT) 0, name);
  122.             dbbind(dbproc, 2, NTBSTRINGBIND, (DBINT) 0, type);
  123.             dbbind(dbproc, 3, INTBIND, (DBINT) 0, (BYTE *) &id);
  124.             dbbind(dbproc, 4, NTBSTRINGBIND, (DBINT) 0, crdate);
  125.  
  126.             /* Print appropriate header for the type of
  127.             * data coming back.
  128.             */
  129.  
  130.             printf("\n %s Objects: \n\n",
  131.             DBCURCMD(dbproc) == 1 ? "System Table": "Procedure");
  132.  
  133.             /* Now print the rows. */
  134.  
  135.             while (dbnextrow(dbproc) != NO_MORE_ROWS)
  136.             {
  137.                 /*
  138.                 ** If this is the 2nd command and
  139.                 ** 10th row, flush the rest of the
  140.                 ** rows for that command.
  141.                 */
  142.  
  143.                 if ((DBCURCMD(dbproc) == 2)
  144.                 && (DBCURROW(dbproc) > 10))
  145.                                     continue;
  146.  
  147.                 printf
  148.                 ("%s %s %ld %s\n", name, type, id, crdate);
  149.             }
  150.         }
  151.     }
  152.  
  153.  
  154.     /* Close our connection and exit the program. */
  155.  
  156.     dbexit();
  157.     return(STDEXIT);
  158. }
  159.  
  160. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  161. DBPROCESS    *dbproc;
  162. int    severity;
  163. int    dberr;
  164. int    oserr;
  165. char    *dberrstr;
  166. char    *oserrstr;
  167. {
  168.     printf("DB-LIBRARY error:\n\t%s\n", dberrstr);
  169.  
  170.     if (oserr != DBNOERR)
  171.         printf("Operating-system error:\n\t%s\n", oserrstr);
  172.  
  173.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  174.         return(INT_EXIT);
  175.  
  176.     return(INT_CANCEL);
  177. }
  178.  
  179. int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
  180. DBPROCESS    *dbproc;
  181. DBINT    msgno;
  182. int    msgstate;
  183. int    severity;
  184. char    *msgtext;
  185. {
  186.     printf
  187.     ("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  188.     msgno, msgstate, severity, msgtext);
  189.     return(0);
  190. }
  191.  
  192.  
  193.