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

  1. /*************************************************************************
  2.  
  3.     PROGRAM: SQLCURS - SQL DBLIB cursors sample program for character
  4.                  based MS-OS/2.
  5.          Copyright (c), 1991 by Microsoft Corp.
  6.  
  7. *************************************************************************/
  8.  
  9.  
  10. #define DBMSOS2     /* must identify operating system environment        */
  11. #include <sqlfront.h>
  12. #include <sqldb.h>    /* DBLIB header files (should always be included    */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16.  
  17. #ifndef NULL
  18. #define        NULL    0
  19. #endif
  20.  
  21. /* Forward declarations for error and message handler */
  22.  
  23. int err_handler(void far *, int,int,int,char far *,char far *);
  24. int msg_handler(void far *,DBINT,int,int,char far *);
  25. void ProcessResults(void);
  26.  
  27. #define NROWS        5        /* Number of rows to be scrolled at a time   */
  28. #define KEYSET        CUR_KEYSET    /* CUR_FORWARD, CUR_DYNAMIC, CUR_KEYSET, int */
  29. #define CONCUROPT   CUR_OPTCC    /* CUR_READONLY, CUR_LOCKCC, CUR_OPTCC, CUR_OPTCCVAL */
  30.  
  31.  
  32. /* Select statement to be used in the cursor case */
  33.  
  34. char stmt[] = " select au_lname, au_fname, city, state from authors where contract = 1 ";
  35.  
  36.  
  37. /* Status array, and results set */
  38.  
  39. DBINT        pstat[NROWS];
  40. char        au_lname[NROWS][41];
  41. char        au_fname[NROWS][21];
  42. char        au_city[NROWS][21];
  43. char        au_state[NROWS][3];
  44. DBINT        au_citlen[NROWS];
  45. DBINT        au_statlen[NROWS];
  46. DBINT        au_fnamlen[NROWS];
  47. char        *stats[NROWS];
  48. char        values[250];
  49.  
  50. main ()
  51. {
  52.     DBPROCESS *dbproc;    /* allocate a DB-LIB process structure        */
  53.     LOGINREC  *login;    /* allocate a DB-LIB login structure        */
  54.     DBCURSOR  *hcursor; /* allocate a DB-LIB cursor structure        */
  55.  
  56.     char       Servername[21] = "";
  57.     RETCODE    result_code;
  58.     int     fetch, optype;
  59.     int     nrow;
  60.     char    sfetch[3], soptype[3];
  61.     char    srow[3], stab[31];
  62.  
  63.  
  64.     /* Install the user-supplied error-handling    and message-handling
  65.      * routines. They are defined at the bottom    of this    source file.
  66.      */
  67.     dberrhandle(err_handler);
  68.     dbmsghandle(msg_handler);
  69.  
  70.     /* Get server's computer name */
  71.     printf ("\nEnter LAN Manager Network Computer Name of SQL SERVER: ");
  72.     gets (Servername);
  73.  
  74.  
  75.     login = dblogin();            /* get login record    from DB-LIB */
  76.     DBSETLUSER (login, (char far *)"sa");     /* set the username    */
  77.     DBSETLAPP (login, (char far *)"curtest"); /* set the application name */
  78.     DBSETLPWD (login, (char far *)"");          /* set the SQL Server password */
  79.  
  80.     /* Now attempt to create and initialize a DBPROCESS    structure */
  81.     if(    (dbproc    = dbopen (login, Servername)) == NULL)
  82.     {
  83.     printf ("dbopen failed\n");
  84.     return (1); /* exit program */
  85.     }
  86.  
  87.     dbuse (dbproc, "pubs");  /* use the "pubs" database */
  88.  
  89.     /* Open the cursor */
  90.     hcursor = dbcursoropen(dbproc, stmt, KEYSET, CONCUROPT, NROWS, pstat);
  91.     if (hcursor == (DBCURSOR *)NULL)
  92.     {
  93.     printf("\ndbcursoropen failed\n");
  94.     return(1);
  95.     }
  96.  
  97.     /* Bind variables */
  98.     result_code = dbcursorbind(hcursor, 1, NTBSTRINGBIND, 41, NULL, (char *)au_lname);
  99.     if (result_code == FAIL)
  100.     {
  101.     printf("\ndbcursorbind failed, column 1\n");
  102.     return(1);
  103.     }
  104.     result_code = dbcursorbind(hcursor, 2, NTBSTRINGBIND, 21, au_fnamlen, (char *)au_fname);
  105.     if (result_code == FAIL)
  106.     {
  107.     printf("\ndbcursorbind failed, column 2\n");
  108.     return(1);
  109.     }
  110.     result_code = dbcursorbind(hcursor, 3, NTBSTRINGBIND, 21, au_citlen, (char *)au_city);
  111.     if (result_code == FAIL)
  112.     {
  113.     printf("\ndbcursorbind failed, column 3\n");
  114.     return(1);
  115.     }
  116.     result_code = dbcursorbind(hcursor, 4, NOBIND, 0, au_statlen, (char *)stats);
  117.     if (result_code == FAIL)
  118.     {
  119.     printf("\ndbcursorbind failed, column 4\n");
  120.     return(1);
  121.     }
  122.     /* Begin transaction. Will exit without committing the transaction so that
  123.     ** none of the data modifications will actually be committed */
  124.     if ((dbcmd(dbproc, "begin transaction") == FAIL) ||
  125.     (dbsqlexec(dbproc) == FAIL) || (dbresults(dbproc) == FAIL))
  126.     {
  127.     printf("\n BEGIN TRAN failed");
  128.     return(1);
  129.     }
  130.  
  131.     /* Now fetch and print the rows */
  132.  
  133.     while (TRUE)
  134.     {
  135.     printf("\n(0-leave, 1-first, 2-next, 3-prev, 4-random, 5-rel, 6-last)");
  136.     printf("\n Enter fetch value");
  137.     fetch = atoi(gets(sfetch));
  138.     if (fetch != 0)
  139.     {
  140.         if ((fetch == FETCH_RANDOM) || (fetch == FETCH_RELATIVE))
  141.         {
  142.         printf("\n Enter row number");
  143.         nrow = atoi(gets(srow));
  144.         }
  145.         else
  146.         {
  147.         nrow = 0;
  148.         }
  149.  
  150.         if (dbcursorfetch(hcursor, fetch, nrow) == FAIL)
  151.         {
  152.         printf("\ndbcursorfetch failed\n");
  153.         }
  154.         ProcessResults();
  155.         continue;        /* Fetch again */
  156.     }
  157.     /* Do updates */
  158.     while (TRUE)
  159.     {
  160.         printf("\n 0-leave,1-update, 2-delete, 3-insert, 4-refresh, 5-lock, >5-cursorfetch");
  161.         printf("\nEnter operation");
  162.         optype = atoi(gets(soptype));
  163.  
  164.         if ((optype == 0) || (optype >5))        /* Exit condition */
  165.         break;
  166.  
  167.         printf("\nEnter bufferno");
  168.         nrow = atoi(gets(srow));
  169.         printf("\nEnter table");
  170.         gets(stab);
  171.         printf("\nEnter values");
  172.         gets(values);
  173.         if (values == (char *)NULL)
  174.         values[0] = '\0';
  175.         if ((result_code=dbcursor(hcursor, optype, nrow, stab, values))
  176.            == FAIL)
  177.         printf("\n dbcursor failed");
  178.         else
  179.         if (optype == CRS_REFRESH)
  180.             ProcessResults();
  181.         else
  182.             printf("\ndbcursor() succeeded");
  183.  
  184.     }        /* Exit dbcursor() loop */
  185.     if (optype == 0)
  186.         break;       /* Close cursor */
  187.     }
  188.  
  189.     result_code = dbcursorclose(hcursor);
  190.     if (result_code == FAIL)
  191.     {
  192.     printf("\ndbcursorclose failed\n");
  193.     exit(1);
  194.     }
  195.  
  196.     dbexit();
  197. }
  198.  
  199. void ProcessResults()
  200. {
  201.     int i, len;
  202.  
  203.     for (i = 0 ; i < NROWS ; i++)
  204.     {    /* Process results */
  205.     if(pstat[i] & FTC_SUCCEED)
  206.     {
  207.         printf("\n Row no. %d: ", i+1);
  208.         printf("\n Author's Name: %s %s ", au_fname[i], au_lname[i]);
  209.         len = 0;
  210.         if (au_statlen[i] != 0) /* This is a NOBIND type */
  211.         {
  212.         len = min(2, (SHORT)au_statlen[i]);
  213.         memcpy(au_state[i], stats[i], len);
  214.         }
  215.         au_state[i][len] = '\0';
  216.         printf("\n City, State: %s, %s", au_city[i], au_state[i]);
  217.     }
  218.     else if (pstat[i] &  FTC_MISSING)
  219.         printf("\n Row no. %d is missing", i+1);
  220.  
  221.     if (pstat[i] & FTC_ENDOFRESULTS)
  222.     {
  223.         printf("\n End of results");
  224.         break;
  225.     }
  226.     if (pstat[i] & FTC_ENDOFKEYSET)
  227.     {
  228.         printf("\nEnd of keyset");
  229.         break;
  230.     }
  231.     }
  232.     return;
  233. }
  234.  
  235.  
  236. int err_handler(dbproc,    severity, dberr, oserr,    dberrstr, oserrstr)
  237. DBPROCESS   far    *dbproc;
  238. int        severity;
  239. int        dberr;
  240. int        oserr;
  241. char        far    *dberrstr;
  242. char        far    *oserrstr;
  243. {
  244.     printf("DB-LIBRARY error:\n\t%Fs\n", dberrstr);
  245.  
  246.     if (oserr != DBNOERR)
  247.         printf("Operating-system error:\n\t%Fs\n", oserrstr);
  248.  
  249.     if ((dbproc == NULL) ||    (DBDEAD(dbproc)))
  250.         return(INT_EXIT);
  251.     else
  252.     {
  253.         return(INT_CANCEL);
  254.     }
  255. }
  256.  
  257. int msg_handler(dbproc,    msgno, msgstate, severity, msgtext)
  258. DBPROCESS    far *dbproc;
  259. DBINT         msgno;
  260. int         msgstate;
  261. int         severity;
  262. char         far *msgtext;
  263. {
  264.     printf
  265.         ("SQL Server message %ld, state %d, severity %d:\n\t%Fs\n",
  266.          msgno,    msgstate, severity, msgtext);
  267.     return(0);
  268. }
  269.