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