home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a063 / 9.img / SAMPLE / DBLIB / EXAMPLE1.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-02  |  5.8 KB  |  208 lines

  1. /*
  2. **    example1.c
  3. **
  4. **    This example illustrates how to send two queries to
  5. **    SQL Server in a command batch.  It binds each set
  6. **    of results and prints the rows.
  7. **
  8. */
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #include <sybfront.h>
  15. #include <sybdb.h>
  16.  
  17. #define DATELEN     26
  18. #define TYPELEN      2
  19.  
  20. /* Function Prototypes */
  21. int             err_handler(DBPROCESS *, int, int, int, char *, char *);
  22. int             msg_handler(DBPROCESS *, DBINT, int, int, char *, char *, 
  23.                                         char *, DBUSMALLINT);
  24. int                   main();
  25.  
  26. main()
  27. {
  28.     DBPROCESS     *dbproc;       /* Our connection with SQL Server. */
  29.     LOGINREC      *login;        /* Our login information. */
  30.  
  31.     /* These are the variables used to store the returning data. */
  32.  
  33.     DBCHAR         crdate[DATELEN+1];
  34.     DBINT          id;
  35.     DBCHAR         name[MAXNAME+1];    /* MAXNAME is defined in
  36.                                         * "sybdb.h" as the maximum
  37.                                         * length for names of database
  38.                                         * objects, such as tables,
  39.                                         * columns, and procedures.
  40.                                         */
  41.     DBCHAR         type[TYPELEN+1];
  42.     RETCODE        result_code;
  43.  
  44.     /* Initialize DB-Library. */
  45.     if (dbinit() == FAIL)
  46.         exit(ERREXIT);
  47.  
  48.     /* Install the user-supplied error-handling and message-handling
  49.      * routines. They are defined at the bottom of this source file.
  50.      */
  51.     dberrhandle(err_handler);
  52.     dbmsghandle(msg_handler);
  53.  
  54.     /*
  55.     ** Get a LOGINREC structure and fill it with the necessary
  56.     ** login information.
  57.     */
  58.  
  59.     login = dblogin();
  60.     DBSETLPWD(login, "server_password");
  61.     DBSETLAPP(login, "example1");
  62.  
  63.     /*
  64.     ** Get a DBPROCESS structure for communicating with SQL Server.
  65.     ** A NULL servername defaults to the server specified by DSQUERY.
  66.     */
  67.  
  68.     dbproc = dbopen(login, NULL);
  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 will check 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" will be 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 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.     exit(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.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  169.         return(INT_EXIT);
  170.     else 
  171.     {
  172.         printf("DB-Library error:\n\t%s\n", dberrstr);
  173.  
  174.         if (oserr != DBNOERR)
  175.             printf("Operating-system error:\n\t%s\n", oserrstr);
  176.  
  177.         return(INT_CANCEL);
  178.     }
  179. }
  180.  
  181. int msg_handler(dbproc, msgno, msgstate, severity, msgtext, 
  182.                 srvname, procname, line)
  183.  
  184. DBPROCESS       *dbproc;
  185. DBINT           msgno;
  186. int               msgstate;
  187. int               severity;
  188. char            *msgtext;
  189. char            *srvname;
  190. char            *procname;
  191. DBUSMALLINT     line;
  192.  
  193. {
  194.     printf ("Msg %ld, Level %d, State %d\n", 
  195.             msgno, severity, msgstate);
  196.  
  197.     if (strlen(srvname) > 0)
  198.         printf ("Server '%s', ", srvname);
  199.     if (strlen(procname) > 0)
  200.         printf ("Procedure '%s', ", procname);
  201.     if (line > 0)
  202.         printf ("Line %d", line);
  203.  
  204.     printf("\n\t%s\n", msgtext);
  205.  
  206.     return(0);
  207. }
  208.