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

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