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

  1. /*    example6.c */
  2. /*
  3. ** This example illustrates opening a data file, inserting data
  4. ** from the file into a newly created table containing several
  5. ** SQL Server datatypes, and updating the table using browse mode
  6. ** techniques.
  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 BUFLEN 2048
  18.  
  19. /* Forward declarations of the error-handling and message-handing
  20. functions. */
  21. int err_handler(DBPROCESS*, int, int, int, char*, char*);
  22. int msg_handler(DBPROCESS*, DBINT, int, int, char*);
  23.  
  24. main()
  25. {
  26.     LOGINREC    *login;
  27.     DBPROCESS    *q_dbproc;    /* This DBPROCESS is used to
  28.     * query the database.
  29.     */
  30.     DBPROCESS    *u_dbproc;    /* This DBPROCESS is used to
  31.     * simultaneously update the database.
  32.     */
  33.     const char  *qualptr;    /* This points to the WHERE clause
  34.     * appropriate for updating q_dbproc's
  35.     * current data row.
  36.     */
  37.     RETCODE    return_code;
  38.     DBTINYINT    age;
  39.     static char    cmdbuf[BUFLEN];
  40.     FILE    *infile;
  41.  
  42.         dbinit();        /* initialize dblib */
  43.  
  44.     /* Install the user-supplied error-handling and message-handling
  45.     * functions. They are defined at the bottom of this source file.
  46.     */
  47.     dbmsghandle((DBMSGHANDLE_PROC)msg_handler);
  48.     dberrhandle((DBERRHANDLE_PROC)err_handler);
  49.  
  50.     /* Allocate and initialize the LOGINREC structure to be used
  51.     * to open a connection to SQL Server.
  52.     */
  53.  
  54.     login = dblogin();
  55.  
  56.     DBSETLUSER(login, "user");
  57.     DBSETLPWD(login, "my_passwd");
  58.     DBSETLAPP(login, "example6");
  59.     DBSETLVERSION(login, DBVER60);
  60.  
  61.     q_dbproc = dbopen(login, "my_server");
  62.     u_dbproc = dbopen(login, "my_server");
  63.  
  64.     printf("Creating the 'alltypes' table.\n");
  65.  
  66.     /* Create a table that contains several SQL Server datatypes. */
  67.     dbcmd(q_dbproc,"if exists(select name from sysobjects where name='alltypes') drop table alltypes ");
  68.  
  69.     dbcmd(q_dbproc,"create table alltypes ");
  70.     dbcmd(q_dbproc,"(age tinyint,");
  71.     dbcmd(q_dbproc,"userid smallint,");
  72.     dbcmd(q_dbproc,"royalty int,");
  73.     dbcmd(q_dbproc,"name char(25),");
  74.     dbcmd(q_dbproc,"title_id varbinary(20),");
  75.     dbcmd(q_dbproc,"us_citizen bit,");
  76.     dbcmd(q_dbproc,"account float,");
  77.     dbcmd(q_dbproc,"title varchar(20),");
  78.     dbcmd(q_dbproc,"manager char(25),");
  79.     dbcmd(q_dbproc,"sales decimal(10,2),");
  80.     dbcmd(q_dbproc,"timestamp)");
  81.  
  82.     dbcmd(q_dbproc, "create unique index index1 on alltypes(userid)");
  83.  
  84.     dbsqlexec(q_dbproc);
  85.     while (dbresults(q_dbproc) != NO_MORE_RESULTS)
  86.     continue;
  87.  
  88.     /* Insert rows of data into the newly created table "alltypes".
  89.     * We will read in the contents of the file and form an
  90.     * INSERT statement.
  91.     */
  92.  
  93.     if ((infile=fopen("datafile","r")) == NULL)
  94.     {
  95.         printf("Unable to open file 'datafile'.\n");
  96.         return(STDEXIT);
  97.     }
  98.  
  99.     printf("Inserting rows into the 'alltypes' table...\n");
  100.  
  101.     while ((fgets(cmdbuf,BUFLEN,infile)) != NULL)
  102.     {
  103.         dbfcmd(q_dbproc,"insert into alltypes \n");
  104.         dbfcmd(q_dbproc,"values(%s, null) \n",cmdbuf);
  105.     }
  106.  
  107.     dbsqlexec(q_dbproc);
  108.  
  109.     /* Process the results of each of the INSERT statements. */
  110.  
  111.     while ((return_code = dbresults(q_dbproc)) != NO_MORE_RESULTS)
  112.     {
  113.         if (return_code == FAIL)
  114.             printf("One of the insert statements failed.\n");
  115.     }
  116.  
  117.     /* Using DB-LIBRARY's browse mode facilities, we'll increment
  118.     * the age of every person in the table.
  119.     */
  120.  
  121.     printf("Updating rows in the 'alltypes' table...\n");
  122.  
  123.     dbcmd(q_dbproc,"select * from alltypes for browse");
  124.     dbsqlexec(q_dbproc);
  125.  
  126.     while ((return_code = dbresults(q_dbproc)) != NO_MORE_RESULTS)
  127.     {
  128.         if (return_code == SUCCEED)
  129.         {
  130.             while (dbnextrow(q_dbproc) != NO_MORE_ROWS)
  131.             {
  132.                 age = *((DBTINYINT *)(dbdata(q_dbproc, 1)));
  133.                 qualptr = dbqual(q_dbproc, -1, "alltypes");
  134.                 dbcmd(u_dbproc, "update alltypes");
  135.                 dbfcmd
  136.                 (u_dbproc, " set age = %d %s", age+1, qualptr);
  137.                 dbsqlexec(u_dbproc);
  138.                 dbresults(u_dbproc);
  139.                 dbfreequal(qualptr);
  140.             }
  141.         }
  142.     }
  143.  
  144.     /* Now, we'll look at the updated contents of the table to
  145.     * verify that the ages were properly incremented.
  146.     */
  147.     printf("Selecting rows from the 'alltypes' table:\n");
  148.     dbcmd(q_dbproc, "select * from alltypes");
  149.     dbsqlexec(q_dbproc);
  150.     dbresults(q_dbproc);
  151.     dbprrow(q_dbproc);
  152.  
  153.     printf("Dropping 'alltypes' table:\n");
  154.     dbcmd(q_dbproc, "drop table alltypes");
  155.     dbsqlexec(q_dbproc);
  156.     dbresults(q_dbproc);
  157.  
  158.     dbexit();
  159.     return(STDEXIT);
  160. }
  161.  
  162. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  163. DBPROCESS    *dbproc;
  164. int    severity;
  165. int    dberr;
  166. int    oserr;
  167. char    *dberrstr;
  168. char    *oserrstr;
  169. {
  170.     printf("DB-LIBRARY error:\n\t%s\n", dberrstr);
  171.  
  172.     if (oserr != DBNOERR)
  173.         printf("Operating-system error:\n\t%s\n", oserrstr);
  174.  
  175.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  176.         return(INT_EXIT);
  177.  
  178.     return(INT_CANCEL);
  179. }
  180.  
  181. int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
  182. DBPROCESS    *dbproc;
  183. DBINT    msgno;
  184. int    msgstate;
  185. int    severity;
  186. char    *msgtext;
  187. {
  188.     printf
  189.     ("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  190.     msgno, msgstate, severity, msgtext);
  191.     return(0);
  192. }
  193.