home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Internet Business Development Kit / PRODUCT_CD.iso / sqlsvr / ptk / i386 / sqltestr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-12  |  4.5 KB  |  143 lines

  1. /*************************************************************************
  2.  
  3.     PROGRAM: SQLTESTR - SQL    Data Server sample program for MS DOS
  4.         Copyright (c),    1988-1995 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.     PDBPROCESS      dbproc;  /* allocate    a DB-LIB process structure        */
  16.     PLOGINREC      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(PDBPROCESS, int, int, int, char*, char*);
  35.     int msg_handler(PDBPROCESS, DBINT, int, int, char*);
  36.  
  37.     if(dbinit() == (char *)NULL)
  38.     {
  39.         printf("Communications layer not loaded\n");
  40.         exit(1);
  41.     }
  42.  
  43.     // Install the user-supplied error-handling    and message-handling
  44.     // routines. They are defined at the bottom    of this    source file.
  45.     dberrhandle((DBERRHANDLE_PROC)err_handler);
  46.     dbmsghandle((DBMSGHANDLE_PROC)msg_handler);
  47.  
  48.     // Get server's computer name
  49.     Servername[0] = NULL;
  50.     printf ("\nEnter Name of SQL Server: ");
  51.     gets (Servername);
  52.  
  53.     login = dblogin();            /* get login record    from DB-LIB */
  54.     DBSETLUSER (login, "sa");        /* set the username            */
  55.     DBSETLAPP (login, "example1");  /* set the application name        */
  56.     DBSETLPWD (login, "");        /* set the SQL Server password  */
  57.     DBSETLVERSION(login,DBVER60);
  58.     /* Now attempt to create and initialize a DBPROCESS    structure */
  59.     if ((dbproc    = dbopen (login, Servername)) == NULL)
  60.     {
  61.         printf ("dbopen failed\n");
  62.         return (1); /* exit program */
  63.     }
  64.  
  65.     dbuse (dbproc, "pubs");  /*    use the    "pubs" database    */
  66.  
  67.     while (TRUE)
  68.     {
  69.         printf ("\nEnter author's last name to retrieve (return to exit): ");
  70.         gets (getname);
  71.  
  72.         /* if only a return was    entered    */
  73.         if (getname[0] == NULL)
  74.             break;
  75.  
  76.         /* construct command buffer to be sent to the SQL server */
  77.         dbcmd (dbproc, "select au_id, au_lname, au_fname, phone,");
  78.         dbcmd (dbproc, " address, city, state, zip");
  79.         dbcmd (dbproc, " from authors");
  80.         dbfcmd (dbproc,    " where au_lname = '%s'",getname);
  81.  
  82.         dbsqlexec (dbproc);  /*    send command buffer to SQL server */
  83.  
  84.         /* now check the results from the SQL server */
  85.         while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS)
  86.         {
  87.             if (result_code == SUCCEED)
  88.             {
  89.                 dbbind (dbproc,    1, NTBSTRINGBIND, (DBINT) 0, id);
  90.                 dbbind (dbproc,    2, NTBSTRINGBIND, (DBINT) 0, au_lname);
  91.                 dbbind (dbproc,    3, NTBSTRINGBIND, (DBINT) 0, au_fname);
  92.                 dbbind (dbproc,    4, NTBSTRINGBIND, (DBINT) 0, phone);
  93.                 dbbind (dbproc,    5, NTBSTRINGBIND, (DBINT) 0, address);
  94.                 dbbind (dbproc,    6, NTBSTRINGBIND, (DBINT) 0, city);
  95.                 dbbind (dbproc,    7, NTBSTRINGBIND, (DBINT) 0, state);
  96.                 dbbind (dbproc,    8, NTBSTRINGBIND, (DBINT) 0, zip);
  97.  
  98.                 /* now process the rows    */
  99.                 while (dbnextrow(dbproc) != NO_MORE_ROWS)
  100.                 {
  101.                     printf ("Author ID:  %s\n",    id);
  102.                     printf ("Last Name:  %s\n",    au_lname);
  103.                     printf ("First Name: %s\n",    au_fname);
  104.                     printf ("Address:    %s\n",    address);
  105.                     printf ("City:       %s\n",    city);
  106.                     printf ("State:      %s\n",    state);
  107.                     printf ("Zip Code:   %s\n",    zip);
  108.                     printf ("Telephone:  %s\n",    phone);
  109.                     printf ("\n");
  110.                 }
  111.             }
  112.             else
  113.             {
  114.                 printf ("Results Failed\n");
  115.                 break;
  116.             }
  117.         }
  118.     } /* while (TRUE) */
  119.  
  120.     /* Close the connection and    exit */
  121.     dbexit();
  122. }
  123.  
  124. int err_handler(PDBPROCESS dbproc, int severity, int dberr, int oserr, char * dberrstr, char * oserrstr)
  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(PDBPROCESS dbproc, DBINT msgno,  int msgstate, int severity, char * msgtext)
  138. {
  139.     printf("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  140.         msgno,    msgstate, severity, msgtext);
  141.     return(0);
  142. }
  143.