home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / snipit.pak / RANGE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  4.8 KB  |  145 lines

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // Range.c
  4. #include "snipit.h"
  5.  
  6. static const char szTblName[] = "stock";
  7. static const char szTblType[] = szPARADOX;
  8.  
  9. //=====================================================================
  10. //  Function:
  11. //          Range();
  12. //
  13. //  Description:
  14. //          This example shows how to limit the accessible records
  15. //          within a table using ranges.
  16. //=====================================================================
  17. void
  18. Range (void)
  19. {
  20.     DBIResult   rslt;           // Return value from IDAPI functions
  21.     hDBIDb      hDb;            // Handle to the database
  22.     hDBICur     hCur;           // Handle to the table
  23.     CURProps    TblProps;       // Table properties
  24.     pBYTE       pRecBufLow;     // Record buffer
  25.     pBYTE       pRecBufHigh;    // Record buffer
  26.     UINT32      uNumRecs = 0;   // Number of records to display
  27.     DFLOAT      fLowRange;      // Low value for the range
  28.     DFLOAT      fHighRange;     // High value for the range
  29.  
  30.     Screen("*** Range Operations Example ***\r\n");
  31.  
  32.     BREAK_IN_DEBUGGER();
  33.  
  34.     Screen("    Initializing IDAPI...");
  35.     if (InitAndConnect(&hDb) != DBIERR_NONE) 
  36.     {
  37.         Screen("\r\n*** End of Example ***");
  38.         return;
  39.     }
  40.  
  41.     Screen("    Setting the database directory...");
  42.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  43.     ChkRslt(rslt, "SetDirectory");
  44.  
  45.     Screen("    Open the %s table...", szTblName);
  46.     rslt = DbiOpenTable(hDb, (pCHAR) szTblName, (pCHAR) szTblType,
  47.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  48.                         xltFIELD, FALSE, NULL, &hCur);
  49.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  50.     {
  51.         CloseDbAndExit(&hDb);
  52.         Screen("\r\n*** End of Example ***");
  53.         return;
  54.     }
  55.  
  56.     // Allocate space for the record buffer.
  57.     rslt = DbiGetCursorProps(hCur, &TblProps);
  58.     ChkRslt(rslt, "GetCursorProps");
  59.  
  60.     pRecBufLow = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
  61.     pRecBufHigh = (pBYTE) malloc(TblProps.iRecBufSize * sizeof(BYTE));
  62.     if ((pRecBufHigh == NULL) || (pRecBufLow == NULL))
  63.     {
  64.         if (pRecBufHigh) free(pRecBufHigh);
  65.         if (pRecBufLow) free(pRecBufLow);
  66.         Screen("    Error - Could not allocate memory.");
  67.         CloseDbAndExit(&hDb);
  68.         Screen("\r\n*** End of example ***");
  69.         return;
  70.     }
  71.  
  72.     fLowRange = 2315.0;  // Lowest stock number to display.
  73.     fHighRange = 5313.0; // Highest stock number to display.
  74.  
  75.     Screen("\r\n    Change the range of the table: only display"
  76.            " records\r\n        which have a 'Stock No' between %.1f"
  77.            " and %.1f", fLowRange, fHighRange);
  78.  
  79.     rslt = DbiPutField(hCur, 1, pRecBufLow, (pBYTE) &fLowRange);
  80.     ChkRslt(rslt, "PutField");
  81.  
  82.     rslt = DbiPutField(hCur, 1, pRecBufHigh, (pBYTE) &fHighRange);
  83.     ChkRslt(rslt, "PutField");
  84.  
  85.     rslt = DbiSetRange(hCur, FALSE, 0, 0, pRecBufLow, FALSE, 0, 0,
  86.                        pRecBufHigh, TRUE);
  87.     ChkRslt(rslt, "SetRange");
  88.  
  89.  
  90.     Screen("\r\n    Display the %s table...", szTblName);
  91.  
  92.     rslt = DbiSetToBegin(hCur);
  93.     ChkRslt(rslt, "SetToBegin");
  94.     DisplayInMemoryTable(hCur, uNumRecs);
  95.  
  96.     Screen("\r\n    Change the range of the table: no range set");
  97.     rslt = DbiResetRange(hCur);
  98.     ChkRslt(rslt, "SetRange");
  99.  
  100.     rslt = DbiSetToBegin(hCur);
  101.     ChkRslt(rslt, "SetToBegin");
  102.  
  103.     Screen("    Display the %s table...", szTblName);
  104.     DisplayInMemoryTable(hCur, uNumRecs);
  105.  
  106.     Screen("\r\n    Change to the secondary index on field two...");
  107.     rslt = DbiSwitchToIndex(&hCur, NULL, NULL, 2, FALSE);
  108.     ChkRslt(rslt, "SwitchToIndex");
  109.  
  110.     fLowRange = 4000.0;  // Lowest vendor number to display.
  111.     fHighRange = 6000.0; // Highest vendor number to display.
  112.     Screen("\r\n    Change the range of the table: only display records\r\n"
  113.            "        which have a Vendor No between %.1f and"
  114.            " %.1f...", fLowRange, fHighRange);
  115.  
  116.     rslt = DbiPutField(hCur, 2, pRecBufLow, (pBYTE) &fLowRange);
  117.     ChkRslt(rslt, "PutField");
  118.  
  119.     rslt = DbiPutField(hCur, 2, pRecBufHigh, (pBYTE) &fHighRange);
  120.     ChkRslt(rslt, "PutField");
  121.  
  122.     rslt = DbiSetRange(hCur, FALSE, 0, 0, pRecBufLow, FALSE, 0, 0,
  123.                        pRecBufHigh, FALSE);
  124.     ChkRslt(rslt, "SetRange");
  125.  
  126.  
  127.     Screen("\r\n    Display the %s table...", szTblName);
  128.  
  129.     rslt = DbiSetToBegin(hCur);
  130.     ChkRslt(rslt, "SetToBegin");
  131.     DisplayInMemoryTable(hCur, uNumRecs);
  132.  
  133.     free(pRecBufLow);
  134.     free(pRecBufHigh);
  135.  
  136.     Screen("\r\n    Close the %s table...", szTblName);
  137.     rslt = DbiCloseCursor(&hCur);
  138.     ChkRslt(rslt, "CloseCursor");
  139.  
  140.     Screen("    Close the Database and exit IDAPI...");
  141.     CloseDbAndExit(&hDb);
  142.  
  143.     Screen("\r\n*** End of Example ***");
  144. }
  145.