home *** CD-ROM | disk | FTP | other *** search
/ ftp.microsoft.com / 2002-07-02_ftp.microsoft.com.zip / developr / ODBC / unsup-ed / LONGRC.C next >
C/C++ Source or Header  |  1993-10-10  |  3KB  |  107 lines

  1. //
  2. //  PROGRAM: longrcv.c
  3. //           (C) 1990-1992 Microsoft Corporation. All rights reserved
  4. //
  5. //  PURPOSE: Demonstrates the use of SQLGetData to receive long data values
  6. //
  7. //  FUNCTIONS:
  8. //        LongRcv()
  9. //
  10. //
  11.  
  12.  
  13. /* This function retrieves the Publisher name from the Publishers
  14.    table. The table has the following 4 columns:
  15.     pub_id  char(4)
  16.     name    varchar(40)
  17.     city    varchar(20)
  18.     state    char(2)
  19.    For purposes of illustration, we assume that the buffer available
  20.    for holding the Publisher name is just 5 characters long. Since
  21.    this name could potentially be 40 characters, we make multiple
  22.    calls to SQLGetData to retrieve all the characters. */
  23.  
  24. #define NAMEBUFLEN 5
  25.  
  26.  
  27. /*
  28.     The following are the henv, hdbc and hstmt which have presumably
  29.     been initialized by SQLAllocEnv, SQLAllocConnect and SQLAllocStmt
  30.     respectively, in another module.
  31. */
  32.  
  33. extern HENV  henv;
  34. extern HDBC  hdbc;
  35. extern HSTMT hstmt;
  36.  
  37. void FAR PASCAL LongRcv()
  38.  
  39. {
  40.  
  41. RETCODE retcode;                // Holds the return code for API calls
  42. UCHAR    szPubName[NAMEBUFLEN];    // To hold Publisher name
  43. SDWORD    cbPubName;
  44.  
  45.  
  46.  
  47.  
  48. /*      
  49.     ExecDirect a select statement which retrieves the Publisher Name
  50.     and City from the Publishers table.
  51. */
  52.  
  53.    retcode = SQLExecDirect(hstmt, "Select pub_name from publishers",
  54.                 SQL_NTS);
  55.  
  56.  
  57.    if (retcode == SQL_SUCCESS) {
  58.  
  59.       while (TRUE) {
  60.  
  61.       /* The following call to SQLFetch only moves the cursor to the
  62.      next row. If a SQLBindCol had been done, the data would have
  63.      been implicitly transferred to the bound locations. */
  64.  
  65.     retcode = SQLFetch(hstmt);
  66.     if (retcode == SQL_ERROR){
  67.  
  68.      /* Something has gone wrong */
  69.  
  70.         handle_error();
  71.     }
  72.     if (retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){
  73.  
  74.       /* Now, we can explicitly get the required data */
  75.  
  76.         retcode = SQLGetData(hstmt, 1, SQL_C_CHAR, szPubName,
  77.                  NAMEBUFLEN, &cbPubName);
  78.  
  79.       /* If the pub_name was longer than 5 characters, this call to
  80.          SQLGetData returns SQL_SUCCESS_WITH_INFO. A subsequent
  81.          call to SQLError returns SQLSTATE 01004 (Data Truncated).
  82.          At this stage we enter a loop to repeatedly call SQLGetData
  83.          until all data has been obtained. */
  84.  
  85.         while (retcode == SQL_SUCCESS_WITH_INFO){
  86.         retcode = SQLGetData(hstmt, 1, SQL_C_CHAR, szPubName,
  87.                  NAMEBUFLEN, &cbPubName);
  88.         }
  89.  
  90.       /* At this stage, if retcode is SQL_SUCCESS, we are fine.
  91.          Otherwise, something wrong has happened! */
  92.  
  93.         if (retcode == SQL_SUCCESS){
  94.         return ;
  95.         }
  96.         handle_error();
  97.  
  98.  
  99.  
  100.     }   //    inner if
  101.  
  102.       }   // while
  103.  
  104.    }   // outer if
  105.  
  106. }
  107.