home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a063 / 9.img / SAMPLE / DBLIB / EXAMPLE2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-02  |  5.3 KB  |  214 lines

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