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