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