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