home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a025 / 11.ddi / EXAMPLE4.C@ / EXAMPLE4.bin
Encoding:
Text File  |  1992-09-15  |  4.4 KB  |  201 lines

  1. /*    example4.c */
  2. /*
  3. ** This example accesses the data within each row without using dbbind()
  4. ** and illustrates the use of row buffering.
  5. **
  6. ** It runs a query, saves all of the returned rows (up to a maximum
  7. ** of 100) using DB-LIBRARY row buffering, and allows the user to
  8. ** examine data rows at random.
  9. */
  10.  
  11. #define DBMSOS2
  12. #include <stdio.h>
  13. #include <sqlfront.h>
  14. #include <sqldb.h>
  15.  
  16. extern char    *malloc();
  17.  
  18. DBPROCESS    *dbproc;    /* Our connection with SQL Server. */
  19. LOGINREC    *login;    /* Our login information. */
  20.  
  21. #define TYPELEN 2
  22. #define DATELEN 25
  23.  
  24. /* Forward declarations of the error handler and message handler.
  25. */
  26. int    err_handler();
  27. int    msg_handler();
  28.  
  29. main(argc, argv)
  30. int    argc;
  31. char    *argv[];
  32. {
  33.     /* Here are the variables which will be used to store the
  34.     * data being examined.
  35.     */
  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.     DBINT    id;
  44.  
  45.     DBCHAR    datebuf[64];
  46.     DBINT    len;
  47.     char    numstring[32];
  48.     int    quitflag = 0;
  49.     RETCODE    row_code;
  50.     DBINT    rownum;
  51.  
  52.     /* Install the user-supplied error-handling and message-handling
  53.     * functions. They are defined at the bottom of this source file.
  54.     */
  55.     dberrhandle(err_handler);
  56.     dbmsghandle(msg_handler);
  57.  
  58.     /*
  59.     ** Get a LOGINREC structure and fill it with the necessary
  60.     ** login information.
  61.     */
  62.  
  63.     login = dblogin();
  64.     DBSETLUSER (login, "user");
  65.     DBSETLPWD(login, "my_passwd");
  66.     DBSETLAPP(login, "example4");
  67.  
  68.     dbproc = dbopen(login, "my_server");
  69.     dbcmd(dbproc, "select name, type, id, crdate from sysobjects");
  70.     dbcmd(dbproc, " where type = 'S'");
  71.  
  72.     /*
  73.         ** Set row buffering to 100 rows, via dbsetopt().
  74.     ** Note that this parameter must be passed as an ASCII string.
  75.     */
  76.  
  77.     dbsetopt(dbproc, DBBUFFER, "100");
  78.     dbsqlexec(dbproc);
  79.     if (dbresults(dbproc) == SUCCEED)
  80.     {
  81.         /* Read all of the rows into DB-LIBRARY's buffer */
  82.         while ((row_code = dbnextrow(dbproc)) != NO_MORE_ROWS)
  83.         {
  84.             /* If DB-LIBRARY's row buffer is full, throw
  85.             * away the oldest row to allow the newest
  86.             * row to be read in.
  87.             */
  88.             if (row_code == BUF_FULL)
  89.                 dbclrbuf(dbproc, (DBINT) 1);
  90.         }
  91.  
  92.         /* Print out the column headers. */
  93.     
  94.         printf
  95.             (" %-20s %10s %25s %15s \n",
  96.             "    NAME    ",
  97.             "TYPE",
  98.             "    DATE    ",
  99.             "ID");
  100.         printf
  101.             (" %20s %10s %25s %15s \n",
  102.             "--------------------",
  103.             "----",
  104.             "----------------------",
  105.             "----");
  106.  
  107.         /* Let the user view any row in the table. */
  108.     
  109.         printf("Type the number of the row you want to see.\n");
  110.         printf("The first row is number 1.\n");
  111.         printf("Asking for row 0 will terminate the program.\n");
  112.  
  113.         while (quitflag == 0)
  114.         {
  115.             printf("Row number: ");
  116.             gets(numstring);
  117.             rownum = atoi(numstring);
  118.  
  119.         if (rownum == 0)
  120.             quitflag = 1;
  121.         else
  122.         {
  123.             /* Print out the requested row. */
  124.             if (dbgetrow(dbproc, rownum) == NO_MORE_ROWS)
  125.                 printf
  126.                 ("That row is not in the table.\n");
  127.             else
  128.             {
  129.                 /* Copy variable-length character data
  130.                 * (colname).
  131.                     */
  132.  
  133.                 strncpy
  134.                 (name, (DBCHAR *)dbdata(dbproc, 1),
  135.                 (len = dbdatlen(dbproc, 1)));
  136.  
  137.                 /* String needs terminating null. */
  138.  
  139.                 name[len] = '\0';
  140.  
  141.                 /* Copy fixed-length character data. */
  142.  
  143.                 strncpy
  144.                 (type, (DBCHAR *)dbdata(dbproc, 2),
  145.                 (len = dbdatlen(dbproc, 2)));
  146.                 type[len] = '\0';
  147.  
  148.                 /* Copy integer data. */
  149.                 id = *((DBINT *)dbdata(dbproc, 3));
  150.  
  151.                 /* Convert datetime data to a printable
  152.                 * string.
  153.                 */
  154.  
  155.                 dbconvert(dbproc, SQLDATETIME, (dbdata(dbproc, 4)),
  156.                 (DBINT)-1, SQLCHAR, datebuf, (DBINT)-1);
  157.                 printf
  158.                 ("%20s %10s %25s %15ld \n",
  159.                 name, type, datebuf, id);
  160.                 }
  161.             }
  162.         }
  163.     }
  164.  
  165.     dbexit();
  166.     exit(STDEXIT);
  167. }
  168.  
  169. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  170. DBPROCESS    *dbproc;
  171. int    severity;
  172. int    dberr;
  173. int    oserr;
  174. char    *dberrstr;
  175. char    *oserrstr;
  176. {
  177.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  178.         return(INT_EXIT);
  179.     else
  180.         {
  181.         printf("DB<MS>-LIBRARY error:\n\t%s\n", dberrstr);
  182.         if (oserr != DBNOERR)
  183.             printf("Operating-system error:\n\t%s\n", oserrstr);
  184.         return(INT_CANCEL);
  185.     }
  186. }
  187.  
  188. int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
  189. DBPROCESS    *dbproc;
  190. DBINT    msgno;
  191. int    msgstate;
  192. int    severity;
  193. char    *msgtext;
  194. {
  195.     printf
  196.     ("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  197.     msgno, msgstate, severity, msgtext);
  198.     return(0);
  199. }
  200.  
  201.