home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / sql / dblib / c / sqlcurs / sqlcurs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-03  |  7.9 KB  |  308 lines

  1. /*************************************************************************
  2.  
  3.     PROGRAM: SQLCURS - SQL DB-Library cursors sample program
  4.          Copyright (c), 1995 by Microsoft Corp.
  5.  
  6. *************************************************************************/
  7.  
  8.  
  9. #if defined(DBNTWIN32)
  10. #include <windows.h>
  11. #endif
  12.  
  13. #include <sqlfront.h>
  14. #include <sqldb.h>    /* DBLIB header files (should always be included    */
  15. #include <stdio.h>
  16. #include <conio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19.  
  20. #ifndef NULL
  21. #define        NULL    0
  22. #endif
  23.  
  24. #define INVALID_VALUE (short)-100
  25.  
  26. /* Forward declarations of the error handler and message handler. */
  27. int err_handler(DBPROCESS*, int, int, int, char*, char*);
  28. int msg_handler(DBPROCESS*, DBINT, int, int, char*);
  29.  
  30. void ProcessResults(void);
  31.  
  32. #define NROWS        5        /* Number of rows to be scrolled at a time   */
  33.  
  34.  
  35. /* Select statement to be used in the cursor case */
  36.  
  37. char stmt[] = " select au_lname, au_fname, city, state from authors where contract = 1 ";
  38.  
  39.  
  40. /* Status array, and results set */
  41.  
  42. DBINT        pstat[NROWS];
  43. char        au_lname[NROWS][41];
  44. char        au_fname[NROWS][21];
  45. char        au_city[NROWS][21];
  46. char        au_state[NROWS][3];
  47. DBINT        au_citlen[NROWS];
  48. DBINT        au_statlen[NROWS];
  49. DBINT        au_fnamlen[NROWS];
  50. char        *stats[NROWS];
  51. char        values[250];
  52. short     ScrollOpt;
  53. short     ConcurOpt;
  54. int       ch;
  55.  
  56. main ()
  57. {
  58.     DBPROCESS *dbproc;    /* allocate a DB-LIB process structure        */
  59.     LOGINREC  *login;    /* allocate a DB-LIB login structure        */
  60.     DBCURSOR  *hcursor; /* allocate a DB-LIB cursor structure        */
  61.  
  62.     char       Servername[21] = "";
  63.     RETCODE    result_code;
  64.     int     fetch, optype;
  65.     int     nrow;
  66.     char    sfetch[3], soptype[3];
  67.     char    srow[3], stab[31];
  68.  
  69.     /* Install the user-supplied error-handling    and message-handling
  70.      * routines. They are defined at the bottom    of this    source file.
  71.      */
  72.     
  73.     dberrhandle((DBERRHANDLE_PROC)err_handler);
  74.     dbmsghandle((DBMSGHANDLE_PROC)msg_handler);
  75.  
  76.     /* Get server's computer name */
  77.     printf ("\nEnter Name of SQL Server: ");
  78.     gets (Servername);
  79.  
  80.     ScrollOpt = INVALID_VALUE;
  81.     do 
  82.     {
  83.         printf ("\nEnter Scroll Option: F)orward  K)eyset  D)ynamic  I)nsensitive: ");
  84.         ch = tolower(_getch());
  85.         switch (ch)
  86.         {
  87.             case 'f': ScrollOpt = CUR_FORWARD; break;
  88.             case 'k': ScrollOpt = CUR_KEYSET;  break;
  89.             case 'd': ScrollOpt = CUR_DYNAMIC; break;
  90.             case 'i': ScrollOpt = CUR_INSENSITIVE; break;
  91.             default:  printf("Invalid character entered."); break;
  92.         }
  93.     } while (ScrollOpt == INVALID_VALUE);
  94.  
  95.     ConcurOpt = INVALID_VALUE;
  96.     do 
  97.     {
  98.         printf ("\nEnter Concurrency Option: R)ead Only   L)ock CC   O)opt CC   V)al Opt CC: ");
  99.         ch = tolower(_getch());
  100.         switch (ch)
  101.         {
  102.             case 'r': ConcurOpt = CUR_READONLY; break;
  103.             case 'l': ConcurOpt = CUR_LOCKCC;   break;
  104.             case 'o': ConcurOpt = CUR_OPTCC;    break;
  105.             case 'v': ConcurOpt = CUR_OPTCCVAL; break;
  106.             default:  printf("Invalid character entered."); break;
  107.         }
  108.     } while (ConcurOpt == INVALID_VALUE);
  109.     printf("\n");
  110.     login = dblogin();            /* get login record    from DB-LIB */
  111.     DBSETLUSER (login, (char far *)"sa");     /* set the username    */
  112.     DBSETLAPP (login, (char far *)"curtest"); /* set the application name */
  113.     DBSETLPWD (login, (char far *)"");          /* set the SQL Server password */
  114.     DBSETLVERSION(login, DBVER60);
  115.  
  116.     /* Now attempt to create and initialize a DBPROCESS    structure */
  117.     if((dbproc = dbopen (login, Servername)) == NULL)
  118.     {
  119.         printf ("dbopen failed\n");
  120.         return (1); /* exit program */
  121.     }
  122.  
  123.     dbuse (dbproc, "pubs");  /* use the "pubs" database */
  124.  
  125.     /* Open the cursor */
  126.     hcursor = dbcursoropen(dbproc, stmt, ScrollOpt, ConcurOpt, NROWS, pstat);
  127.     if (hcursor == (DBCURSOR *)NULL)
  128.     {
  129.         printf("\ndbcursoropen failed\n");
  130.         return(1);
  131.     }
  132.  
  133.     /* Bind variables */
  134.     result_code = dbcursorbind(hcursor, 1, NTBSTRINGBIND, 41, NULL, (char *)au_lname);
  135.     if (result_code == FAIL)
  136.     {
  137.         printf("\ndbcursorbind failed, column 1\n");
  138.         return(1);
  139.     }
  140.     result_code = dbcursorbind(hcursor, 2, NTBSTRINGBIND, 21, au_fnamlen, (char *)au_fname);
  141.     if (result_code == FAIL)
  142.     {
  143.         printf("\ndbcursorbind failed, column 2\n");
  144.         return(1);
  145.     }
  146.     result_code = dbcursorbind(hcursor, 3, NTBSTRINGBIND, 21, au_citlen, (char *)au_city);
  147.     if (result_code == FAIL)
  148.     {
  149.         printf("\ndbcursorbind failed, column 3\n");
  150.         return(1);
  151.     }
  152.     
  153.     result_code = dbcursorbind(hcursor, 4, NOBIND, 0, au_statlen, (char *)stats);
  154.     if (result_code == FAIL)
  155.     {
  156.         printf("\ndbcursorbind failed, column 4\n");
  157.         return(1);
  158.     }
  159.     
  160.     /* Begin transaction. Will exit without committing the transaction so that
  161.     ** none of the data modifications will actually be committed */
  162.     if ((dbcmd(dbproc, "begin transaction") == FAIL) ||
  163.         (dbsqlexec(dbproc) == FAIL) || (dbresults(dbproc) == FAIL))
  164.     {
  165.         printf("\n BEGIN TRAN failed");
  166.         return(1);
  167.     }
  168.  
  169.     /* Now fetch and print the rows */
  170.     while (TRUE)
  171.     {
  172.         printf("\n0)Leave  1)First  2)Next  3)Previous  4)Random  5)Relative  6)Last  7)By Value");
  173.         printf("\nEnter fetch value: ");
  174.         fetch = atoi(gets(sfetch));
  175.         if (fetch != 0)
  176.         {
  177.             if ((fetch == FETCH_RANDOM) || (fetch == FETCH_RELATIVE))
  178.             {
  179.                 printf("\n Enter row number: ");
  180.                 nrow = atoi(gets(srow));
  181.             }
  182.             else
  183.                 nrow = 0;
  184.  
  185.             if (dbcursorfetchex(hcursor, fetch, nrow, NROWS, 0) == FAIL)
  186.                 printf("\ndbcursorfetchex() failed\n");
  187.  
  188.             ProcessResults();
  189.             continue;        /* Fetch again */
  190.         }
  191.     
  192.         /* Do updates */
  193.         while (TRUE)
  194.         {
  195.             printf("\n0)Leave  1)Update  2)Delete  3)Insert  4)Refresh  5)Lock  6)Cursor Fetch");
  196.             printf("\nEnter operation: ");
  197.             optype = atoi(gets(soptype));
  198.  
  199.             if ((optype == 0) || (optype == 6))        /* Exit condition */
  200.                 break;
  201.             
  202.             if (optype > 6)
  203.                 continue;
  204.  
  205.             printf("\nEnter buffer number: ");
  206.             nrow = atoi(gets(srow));
  207.             printf("\nEnter table: ");
  208.             gets(stab);
  209.             printf("\nEnter values: ");
  210.             gets(values);
  211.             if (values == (char *)NULL)
  212.                 values[0] = '\0';
  213.             if ((result_code=dbcursor(hcursor, optype, nrow, stab, values)) == FAIL)
  214.                 printf("\n dbcursor failed");
  215.             else
  216.                 if (optype == CRS_REFRESH)
  217.                     ProcessResults();
  218.                 else
  219.                     printf("\ndbcursor() succeeded");
  220.  
  221.         } /* Exit dbcursor() loop */
  222.         
  223.         if (optype == 0)
  224.             break;       /* Close cursor */
  225.     }
  226.  
  227.     dbcursorclose(hcursor);
  228.  
  229.     dbexit();
  230. }
  231.  
  232. void ProcessResults()
  233. {
  234.     int i, len;
  235.  
  236.     printf(" Row       First Name                 Last Name                City       State\n");
  237.     printf("----- -------------------- ------------------------------ --------------- -----\n");
  238.     for (i = 0 ; i < NROWS ; i++)
  239.     {    /* Process results */
  240.         if(pstat[i] & FTC_SUCCEED)
  241.         {
  242.             len = 0;
  243.             if (au_statlen[i] != 0) /* This is a NOBIND type */
  244.             {
  245.                 len = min(2, (SHORT)au_statlen[i]);
  246.                 memcpy(au_state[i], stats[i], len);
  247.             }
  248.             au_state[i][len] = '\0';
  249.               printf("%5d %-20s %-30s %-15s %-2s\n",i+1,au_fname[i],au_lname[i],au_city[i],au_state[i]);
  250.         }
  251.         else
  252.             if (pstat[i] &  FTC_MISSING)
  253.                 printf("\n Row no. %d is missing", i+1);
  254.  
  255.         /*
  256.         
  257.         The following code could be used if dbcursorfetch()
  258.         was used instead of dbcursorfetchex().
  259.  
  260.         if (pstat[i] & FTC_ENDOFRESULTS)
  261.         {
  262.             printf("\n End of results");
  263.             break;
  264.         }
  265.         if (pstat[i] & FTC_ENDOFKEYSET)
  266.         {
  267.             printf("\nEnd of keyset");
  268.             break;
  269.         }
  270.         */
  271.  
  272.     }
  273.     return;
  274. }
  275.  
  276. int err_handler(dbproc,    severity, dberr, oserr,    dberrstr, oserrstr)
  277. DBPROCESS *dbproc;
  278. int         severity;
  279. int         dberr;
  280. int         oserr;
  281. char        *dberrstr;
  282. char        *oserrstr;
  283. {
  284.     printf("DB-LIBRARY error:\n\t%s\n", dberrstr);
  285.  
  286.     if (oserr != DBNOERR)
  287.         printf("Operating-system error:\n\t%s\n", oserrstr);
  288.  
  289.     if ((dbproc == NULL) ||    (DBDEAD(dbproc)))
  290.         return(INT_EXIT);
  291.     else
  292.     {
  293.         return(INT_CANCEL);
  294.     }
  295. }
  296.  
  297. int msg_handler(dbproc,    msgno, msgstate, severity, msgtext)
  298. DBPROCESS *dbproc;
  299. DBINT         msgno;
  300. int         msgstate;
  301. int         severity;
  302. char        *msgtext;
  303. {
  304.     printf("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  305.         msgno,    msgstate, severity, msgtext);
  306.     return(0);
  307. }
  308.