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

  1. /*    example2.c */
  2. /*
  3. ** This example opens a data file, inserts data from the file
  4. ** into a newly created table containing several of the
  5. ** SQL Server datatypes, and binds and prints the results.
  6. ** You must have a file named datafile and CREATE DATABASE
  7. ** permission in your login database.  Also you must have enough room
  8. ** in your default device to create a new database (minimum 2Mb).
  9. */
  10.  
  11. #if defined(DBNTWIN32)
  12. #include <windows.h>
  13. #endif
  14.  
  15. #include <stdio.h>
  16. #include <sqlfront.h>
  17. #include <sqldb.h>
  18.  
  19. #define BUFLEN    2048
  20. #define HEXLEN    510
  21. #define PLEN    25
  22. #define SALESLEN 11
  23.  
  24. /* Forward declarations of the error handler and message handler functions.
  25. */
  26. int err_handler(DBPROCESS*, int, int, int, char*, char*);
  27. int msg_handler(DBPROCESS*, DBINT, int, int, char*);
  28.  
  29. main(argc,argv)
  30. int    argc;
  31. char    *argv[];
  32. {
  33.  
  34.     LOGINREC    *login;
  35.     DBPROCESS    *dbproc;
  36.     RETCODE    return_code;
  37.  
  38.     DBTINYINT    age;
  39.     DBSMALLINT    userid;
  40.     DBINT    royalty;
  41.     DBCHAR    name[PLEN+1];
  42.     DBBINARY    title_id[PLEN+1];
  43.     DBBIT    us_citizen;
  44.     DBFLT8    account;
  45.     DBCHAR    title[PLEN+1];    /* string    */
  46.     DBCHAR    manager[PLEN+1];    /* ntbstring */
  47.     DBCHAR    id_buffer[HEXLEN+1];
  48.     DBCHAR   sales_buffer[SALESLEN+1];
  49.     DBDECIMAL sales;
  50.  
  51.     static char    cmdbuf[BUFLEN];
  52.     FILE    *infile;
  53.  
  54.         dbinit();        /* initialize dblib */
  55.  
  56.  
  57.     /* Install the user-supplied error-handling and message-handling
  58.     * functions. They are defined at the bottom of this source file.
  59.     */
  60.  
  61.     dbmsghandle((DBMSGHANDLE_PROC)msg_handler);
  62.     dberrhandle((DBERRHANDLE_PROC)err_handler);
  63.  
  64.     /* Allocate and initialize the LOGINREC structure to be used
  65.     * to open a connection to SQL Server.
  66.     */
  67.  
  68.     login = dblogin();
  69.     DBSETLUSER(login, "user");
  70.     DBSETLPWD(login, "my_passwd");
  71.     DBSETLAPP(login, "example2");
  72.     DBSETLVERSION(login, DBVER60);
  73.  
  74.     dbproc = dbopen(login, "my_server");
  75.     printf("Creating the 'test' database.\n");
  76.     dbcmd(dbproc,"create database test ");
  77.     dbsqlexec(dbproc);
  78.     dbresults(dbproc);
  79.     dbuse(dbproc,"test");
  80.     printf("Creating the 'alltypes' table.\n");
  81.  
  82.     /* Create a table that contains several SQL Server datatypes. */
  83.     dbcmd(dbproc,"create table alltypes ");
  84.     dbcmd(dbproc,"(age tinyint,");
  85.     dbcmd(dbproc,"userid smallint,");
  86.     dbcmd(dbproc,"royalty int,");
  87.     dbcmd(dbproc,"name char(25),");
  88.     dbcmd(dbproc,"title_id varbinary(20),");
  89.     dbcmd(dbproc,"us_citizen bit,");
  90.     dbcmd(dbproc,"account float,");
  91.     dbcmd(dbproc,"title varchar(20),");
  92.     dbcmd(dbproc,"manager char(25),");
  93.     dbcmd(dbproc,"sales decimal(10,2))");
  94.     dbsqlexec(dbproc);
  95.     dbresults(dbproc);
  96.  
  97.     /* Insert rows of data into the newly created table "alltypes".
  98.     * We will read in the contents of the file and form an
  99.     * INSERT statement.
  100.     */
  101.  
  102.     if ((infile=fopen("datafile","r")) == NULL)
  103.     {
  104.         printf("Unable to open file 'datafile'.\n");
  105.         return(STDEXIT);
  106.     }
  107.  
  108.     printf("Inserting rows into the 'alltypes' table.\n");
  109.  
  110.     while ((fgets(cmdbuf,BUFLEN,infile)) != NULL)
  111.     {
  112.         dbfcmd(dbproc,"insert into alltypes \n");
  113.         dbfcmd(dbproc,"values(%s) \n",cmdbuf);
  114.     }
  115.     dbsqlexec(dbproc);
  116.  
  117.     /* Process the results of each of the INSERT statements. */
  118.     while ((return_code = dbresults(dbproc)) != NO_MORE_RESULTS)
  119.     {
  120.         if (return_code == FAIL)
  121.             printf("One of the insert statements failed.\n");
  122.     }
  123.     printf("Selecting rows from the 'alltypes' table:\n");
  124.     dbcmd(dbproc,"select * from alltypes");
  125.     dbsqlexec(dbproc);
  126.  
  127.     /* Initialize the DBDECIMAL structure so binding and conversions will use defaults */
  128.     sales.precision = 0;
  129.     sales.scale = 0;
  130.  
  131.     while ((return_code = dbresults(dbproc)) != NO_MORE_RESULTS)
  132.     {
  133.         if (return_code == SUCCEED)
  134.         {
  135.             dbbind(dbproc,1,TINYBIND, (DBINT) 0,(BYTE *) &age);
  136.             dbbind(dbproc,2,SMALLBIND, (DBINT) 0,(BYTE *) &userid);
  137.             dbbind(dbproc,3,INTBIND, (DBINT) 0,(BYTE *) &royalty);
  138.             dbbind(dbproc,4,CHARBIND, (DBINT) 0,name);
  139.             dbbind(dbproc,5,BINARYBIND, (DBINT) 0,title_id);
  140.             dbbind(dbproc,6,BITBIND, (DBINT) 0,(BYTE *) &us_citizen);
  141.             dbbind(dbproc,7,FLT8BIND, (DBINT) 0,(BYTE *) &account);
  142.             dbbind(dbproc,8,STRINGBIND, (DBINT) 0,title);
  143.             dbbind(dbproc,9,NTBSTRINGBIND, (DBINT) 0,manager);
  144.             dbbind(dbproc,10,DECIMALBIND, (DBINT) 0, (BYTE *)&sales);
  145.  
  146.             /*
  147.             ** Initialize null terminator in "name" array,
  148.             ** since CHARBIND does not add one.
  149.             */
  150.             name[PLEN] = '\0';
  151.  
  152.             while (dbnextrow(dbproc) != NO_MORE_ROWS)
  153.             {
  154.                 dbconvert (dbproc, SQLBINARY, title_id, dbdatlen(dbproc, 5), SQLCHAR, id_buffer, (DBINT)-1);
  155.                 dbconvert (dbproc, SQLDECIMAL, (BYTE *)&sales, sizeof(DBDECIMAL), SQLCHAR, sales_buffer, (DBINT)-1);
  156.  
  157.                 printf
  158.                 ("%d    %d    %ld    %s    0x%s\n",
  159.                 age, userid, royalty, name, id_buffer);
  160.                 printf
  161.                 ("%d    %8.2f    %s    %s %s\n",
  162.                 us_citizen, account, title, manager, sales_buffer);
  163.                 
  164.             }
  165.         }
  166.     }
  167.     dbexit();
  168.     return(STDEXIT);
  169. }
  170.  
  171. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  172. DBPROCESS    *dbproc;
  173. int    severity;
  174. int    dberr;
  175. int    oserr;
  176. char    *dberrstr;
  177. char    *oserrstr;
  178. {
  179.     printf("DB-LIBRARY error:\n\t%s\n", dberrstr);
  180.  
  181.     if (oserr != DBNOERR)
  182.         printf("Operating-system error:\n\t%s\n", oserrstr);
  183.  
  184.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  185.         return(INT_EXIT);
  186.  
  187.     return(INT_CANCEL);
  188. }
  189.  
  190. int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
  191. DBPROCESS    *dbproc;
  192. DBINT    msgno;
  193. int    msgstate;
  194. int    severity;
  195. char    *msgtext;
  196. {
  197.     printf
  198.     ("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  199.     msgno, msgstate, severity, msgtext);
  200.     return(0);
  201. }
  202.  
  203.