home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a025 / 11.ddi / SQLTESTP.C@ / SQLTESTP.bin
Encoding:
Text File  |  1992-09-15  |  4.8 KB  |  150 lines

  1. /*************************************************************************
  2.  
  3.     PROGRAM: SQLTEST - SQL Data Server sample program for character
  4.                  based MS-OS/2.
  5.          Copyright (c),    1988-1990 by Microsoft Corp.
  6.  
  7. *************************************************************************/
  8.  
  9. #define    DBMSOS2        /* must    identify operating system environment      */
  10. #include <sqlfront.h>
  11. #include <sqldb.h>    /* DB-LIB header file (should always be    included) */
  12.  
  13. #define        NULL    0
  14.  
  15. main ()
  16. {
  17.     DBPROCESS far *dbproc;  /* allocate    a DB-LIB process structure        */
  18.     LOGINREC  far *login;   /* allocate    a DB-LIB login structure        */
  19.     int           errno;   /* variable    to store DB-LIB    error number in        */
  20.     char      far *msg;        /* used to receive DB-LIB error message pointer */
  21.  
  22.     /* Variables used to store the returning data */
  23.     char       au_lname[41];
  24.     char       au_fname[20];
  25.     char       id[12];
  26.     char       phone[13];
  27.     char       address[41];
  28.     char       city[21];
  29.     char       state[3];
  30.     char       zip[6];
  31.     char       getname[41];
  32.     char       Servername[25];
  33.     RETCODE    result_code;
  34.  
  35.     /* Forward declarations of the error handler and message handler. */
  36.     int           err_handler();
  37.     int           msg_handler();
  38.  
  39.     /* Install the user-supplied error-handling    and message-handling
  40.      * routines. They are defined at the bottom    of this    source file.
  41.      */
  42.     dberrhandle(err_handler);
  43.     dbmsghandle(msg_handler);
  44.  
  45.     /* Get server's computer name */
  46.     Servername[0] = NULL;
  47.     printf ("\nEnter LAN Manager Network Computer Name of SQL SERVER: ");
  48.     gets (Servername);
  49.  
  50.     login = dblogin();            /* get login record    from DB-LIB */
  51.     DBSETLUSER (login, (char far *)"sa");    /* set the username        */
  52.     DBSETLAPP (login, (char far    *)"sqltestp");    /* set the application name    */
  53.     DBSETLPWD (login, (char far    *)"");        /* set the SQL Server password    */
  54.  
  55.     /* Now attempt to create and initialize a DBPROCESS    structure */
  56.     if(    (dbproc    = dbopen (login, Servername)) == NULL)
  57.     {
  58.     printf ("dbopen failed\n");
  59.     return (1); /* exit program */
  60.     }
  61.  
  62.     dbuse (dbproc, "pubs");  /*    use the    "pubs" database    */
  63.  
  64.     while (TRUE)
  65.     {
  66.     printf ("\nEnter author's last name to retrieve (return to exit): ");
  67.     gets (getname);
  68.  
  69.     if (getname[0] == NULL)    break;    /* if only a return was    entered    */
  70.  
  71.     /* construct command buffer to be sent to the SQL server */
  72.     dbcmd (dbproc, (char far *)"select au_id, au_lname, au_fname, phone,");
  73.     dbcmd (dbproc, (char far *)" address, city, state, zip");
  74.     dbcmd (dbproc, (char far *)" from authors");
  75.     dbfcmd (dbproc,    (char far *)" where au_lname = '%Fs'",(char far    *)getname);
  76.  
  77.     dbsqlexec (dbproc);  /*    send command buffer to SQL server */
  78.  
  79.     /* now check the results from the SQL server */
  80.     while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS)
  81.         {
  82.         if (result_code == SUCCEED)
  83.         {
  84.         dbbind (dbproc,    1, NTBSTRINGBIND, (DBINT) 0, (char far *)id);
  85.         dbbind (dbproc,    2, NTBSTRINGBIND, (DBINT) 0, (char far *)au_lname);
  86.         dbbind (dbproc,    3, NTBSTRINGBIND, (DBINT) 0, (char far *)au_fname);
  87.         dbbind (dbproc,    4, NTBSTRINGBIND, (DBINT) 0, (char far *)phone);
  88.         dbbind (dbproc,    5, NTBSTRINGBIND, (DBINT) 0, (char far *)address);
  89.         dbbind (dbproc,    6, NTBSTRINGBIND, (DBINT) 0, (char far *)city);
  90.         dbbind (dbproc,    7, NTBSTRINGBIND, (DBINT) 0, (char far *)state);
  91.         dbbind (dbproc,    8, NTBSTRINGBIND, (DBINT) 0, (char far *)zip);
  92.  
  93.         /* now process the rows    */
  94.         while (dbnextrow(dbproc) != NO_MORE_ROWS)
  95.             {
  96.             printf ("Author ID:  %s\n",    id);
  97.             printf ("Last Name:  %s\n",    au_lname);
  98.             printf ("First Name: %s\n",    au_fname);
  99.             printf ("Address:    %s\n",    address);
  100.             printf ("City:       %s\n",    city);
  101.             printf ("State:      %s\n",    state);
  102.             printf ("Zip Code:   %s\n",    zip);
  103.             printf ("Telephone:  %s\n",    phone);
  104.             printf ("\n");
  105.             }
  106.         }
  107.         else
  108.         {
  109.         printf ("Results Failed\n");
  110.         break;
  111.         }
  112.         }
  113.     }   /* while (TRUE) */
  114.  
  115.     /* Close the connection and    exit */
  116.     dbexit();
  117. }
  118.  
  119. int err_handler(dbproc,    severity, dberr, oserr,    dberrstr, oserrstr)
  120. DBPROCESS   far    *dbproc;
  121. int        severity;
  122. int        dberr;
  123. int        oserr;
  124. char        far    *dberrstr;
  125. char        far    *oserrstr;
  126. {
  127.        printf("DB-LIBRARY error:\n\t%Fs\n", dberrstr);
  128.  
  129.        if (oserr != DBNOERR)
  130.               printf("Operating-system error:\n\t%Fs\n", oserrstr);
  131.  
  132.        if ((dbproc == NULL) ||    (DBDEAD(dbproc)))
  133.            return(INT_EXIT);
  134.        else
  135.            return(INT_CANCEL);
  136. }
  137.  
  138. int msg_handler(dbproc,    msgno, msgstate, severity, msgtext)
  139. DBPROCESS    far *dbproc;
  140. DBINT         msgno;
  141. int         msgstate;
  142. int         severity;
  143. char         far *msgtext;
  144. {
  145.     printf
  146.         ("SQL Server message %ld, state %d, severity %d:\n\t%Fs\n",
  147.          msgno,    msgstate, severity, msgtext);
  148.     return(0);
  149. }
  150.