home *** CD-ROM | disk | FTP | other *** search
- /*************************************************************************
-
- PROGRAM: SQLCURS - SQL DBLIB cursors sample program for character
- based MS-OS/2.
- Copyright (c), 1991 by Microsoft Corp.
-
- *************************************************************************/
-
-
- #define DBMSOS2 /* must identify operating system environment */
- #include <sqlfront.h>
- #include <sqldb.h> /* DBLIB header files (should always be included */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #ifndef NULL
- #define NULL 0
- #endif
-
- /* Forward declarations for error and message handler */
-
- int err_handler(void far *, int,int,int,char far *,char far *);
- int msg_handler(void far *,DBINT,int,int,char far *);
- void ProcessResults(void);
-
- #define NROWS 5 /* Number of rows to be scrolled at a time */
- #define KEYSET CUR_KEYSET /* CUR_FORWARD, CUR_DYNAMIC, CUR_KEYSET, int */
- #define CONCUROPT CUR_OPTCC /* CUR_READONLY, CUR_LOCKCC, CUR_OPTCC, CUR_OPTCCVAL */
-
-
- /* Select statement to be used in the cursor case */
-
- char stmt[] = " select au_lname, au_fname, city, state from authors where contract = 1 ";
-
-
- /* Status array, and results set */
-
- DBINT pstat[NROWS];
- char au_lname[NROWS][41];
- char au_fname[NROWS][21];
- char au_city[NROWS][21];
- char au_state[NROWS][3];
- DBINT au_citlen[NROWS];
- DBINT au_statlen[NROWS];
- DBINT au_fnamlen[NROWS];
- char *stats[NROWS];
- char values[250];
-
- main ()
- {
- DBPROCESS *dbproc; /* allocate a DB-LIB process structure */
- LOGINREC *login; /* allocate a DB-LIB login structure */
- DBCURSOR *hcursor; /* allocate a DB-LIB cursor structure */
-
- char Servername[21] = "";
- RETCODE result_code;
- int fetch, optype;
- int nrow;
- char sfetch[3], soptype[3];
- char srow[3], stab[31];
-
-
- /* Install the user-supplied error-handling and message-handling
- * routines. They are defined at the bottom of this source file.
- */
- dberrhandle(err_handler);
- dbmsghandle(msg_handler);
-
- /* Get server's computer name */
- printf ("\nEnter LAN Manager Network Computer Name of SQL SERVER: ");
- gets (Servername);
-
-
- login = dblogin(); /* get login record from DB-LIB */
- DBSETLUSER (login, (char far *)"sa"); /* set the username */
- DBSETLAPP (login, (char far *)"curtest"); /* set the application name */
- DBSETLPWD (login, (char far *)""); /* set the SQL Server password */
-
- /* Now attempt to create and initialize a DBPROCESS structure */
- if( (dbproc = dbopen (login, Servername)) == NULL)
- {
- printf ("dbopen failed\n");
- return (1); /* exit program */
- }
-
- dbuse (dbproc, "pubs"); /* use the "pubs" database */
-
- /* Open the cursor */
- hcursor = dbcursoropen(dbproc, stmt, KEYSET, CONCUROPT, NROWS, pstat);
- if (hcursor == (DBCURSOR *)NULL)
- {
- printf("\ndbcursoropen failed\n");
- return(1);
- }
-
- /* Bind variables */
- result_code = dbcursorbind(hcursor, 1, NTBSTRINGBIND, 41, NULL, (char *)au_lname);
- if (result_code == FAIL)
- {
- printf("\ndbcursorbind failed, column 1\n");
- return(1);
- }
- result_code = dbcursorbind(hcursor, 2, NTBSTRINGBIND, 21, au_fnamlen, (char *)au_fname);
- if (result_code == FAIL)
- {
- printf("\ndbcursorbind failed, column 2\n");
- return(1);
- }
- result_code = dbcursorbind(hcursor, 3, NTBSTRINGBIND, 21, au_citlen, (char *)au_city);
- if (result_code == FAIL)
- {
- printf("\ndbcursorbind failed, column 3\n");
- return(1);
- }
- result_code = dbcursorbind(hcursor, 4, NOBIND, 0, au_statlen, (char *)stats);
- if (result_code == FAIL)
- {
- printf("\ndbcursorbind failed, column 4\n");
- return(1);
- }
- /* Begin transaction. Will exit without committing the transaction so that
- ** none of the data modifications will actually be committed */
- if ((dbcmd(dbproc, "begin transaction") == FAIL) ||
- (dbsqlexec(dbproc) == FAIL) || (dbresults(dbproc) == FAIL))
- {
- printf("\n BEGIN TRAN failed");
- return(1);
- }
-
- /* Now fetch and print the rows */
-
- while (TRUE)
- {
- printf("\n(0-leave, 1-first, 2-next, 3-prev, 4-random, 5-rel, 6-last)");
- printf("\n Enter fetch value");
- fetch = atoi(gets(sfetch));
- if (fetch != 0)
- {
- if ((fetch == FETCH_RANDOM) || (fetch == FETCH_RELATIVE))
- {
- printf("\n Enter row number");
- nrow = atoi(gets(srow));
- }
- else
- {
- nrow = 0;
- }
-
- if (dbcursorfetch(hcursor, fetch, nrow) == FAIL)
- {
- printf("\ndbcursorfetch failed\n");
- }
- ProcessResults();
- continue; /* Fetch again */
- }
- /* Do updates */
- while (TRUE)
- {
- printf("\n 0-leave,1-update, 2-delete, 3-insert, 4-refresh, 5-lock, >5-cursorfetch");
- printf("\nEnter operation");
- optype = atoi(gets(soptype));
-
- if ((optype == 0) || (optype >5)) /* Exit condition */
- break;
-
- printf("\nEnter bufferno");
- nrow = atoi(gets(srow));
- printf("\nEnter table");
- gets(stab);
- printf("\nEnter values");
- gets(values);
- if (values == (char *)NULL)
- values[0] = '\0';
- if ((result_code=dbcursor(hcursor, optype, nrow, stab, values))
- == FAIL)
- printf("\n dbcursor failed");
- else
- if (optype == CRS_REFRESH)
- ProcessResults();
- else
- printf("\ndbcursor() succeeded");
-
- } /* Exit dbcursor() loop */
- if (optype == 0)
- break; /* Close cursor */
- }
-
- result_code = dbcursorclose(hcursor);
- if (result_code == FAIL)
- {
- printf("\ndbcursorclose failed\n");
- exit(1);
- }
-
- dbexit();
- }
-
- void ProcessResults()
- {
- int i, len;
-
- for (i = 0 ; i < NROWS ; i++)
- { /* Process results */
- if(pstat[i] & FTC_SUCCEED)
- {
- printf("\n Row no. %d: ", i+1);
- printf("\n Author's Name: %s %s ", au_fname[i], au_lname[i]);
- len = 0;
- if (au_statlen[i] != 0) /* This is a NOBIND type */
- {
- len = min(2, (SHORT)au_statlen[i]);
- memcpy(au_state[i], stats[i], len);
- }
- au_state[i][len] = '\0';
- printf("\n City, State: %s, %s", au_city[i], au_state[i]);
- }
- else if (pstat[i] & FTC_MISSING)
- printf("\n Row no. %d is missing", i+1);
-
- if (pstat[i] & FTC_ENDOFRESULTS)
- {
- printf("\n End of results");
- break;
- }
- if (pstat[i] & FTC_ENDOFKEYSET)
- {
- printf("\nEnd of keyset");
- break;
- }
- }
- return;
- }
-
-
- int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
- DBPROCESS far *dbproc;
- int severity;
- int dberr;
- int oserr;
- char far *dberrstr;
- char far *oserrstr;
- {
- printf("DB-LIBRARY error:\n\t%Fs\n", dberrstr);
-
- if (oserr != DBNOERR)
- printf("Operating-system error:\n\t%Fs\n", oserrstr);
-
- if ((dbproc == NULL) || (DBDEAD(dbproc)))
- return(INT_EXIT);
- else
- {
- return(INT_CANCEL);
- }
- }
-
- int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
- DBPROCESS far *dbproc;
- DBINT msgno;
- int msgstate;
- int severity;
- char far *msgtext;
- {
- printf
- ("SQL Server message %ld, state %d, severity %d:\n\t%Fs\n",
- msgno, msgstate, severity, msgtext);
- return(0);
- }
-