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

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // LnkCrsr.c
  4. #include "snipit.h"
  5.  
  6. static const char szMasterTable[] = "customer";
  7. static const char szDetailTable[] = "orders";
  8. static const char szTblType[] = szPARADOX;
  9.  
  10. //=====================================================================
  11. //  Function:
  12. //          LnkCrsr();
  13. //
  14. //  Description:
  15. //          Linking cursors establishes a link between two cursors
  16. //          such that the detail cursor has its record set limited to
  17. //          the set of records matching the linking key values of the
  18. //          master cursor.
  19. //=====================================================================
  20. void
  21. LnkCrsr (void)
  22. {
  23.     hDBIDb      hDb;                // Handle to the database
  24.     hDBICur     hCurMaster;         // Handle to the master table
  25.     hDBICur     hCurDetail;         // Handle to the detail table
  26.     UINT32      ulNumRecs = 10;     // Number of records to display,
  27.                                     //   0 = all
  28.     UINT16 uFieldsMaster[] = {1};   // Link the first field of Customer
  29.     UINT16 uFieldsDetail[] = {2};   // To the second field of Orders
  30.                                     //   (which is indexed)
  31.     DBIResult   rslt;               // Return value from IDAPI functions
  32.  
  33.     Screen("*** Linked Cursors Example ***\r\n");
  34.  
  35.     BREAK_IN_DEBUGGER();
  36.  
  37.     Screen("    Initializing the engine...");
  38.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  39.     {
  40.         Screen("\r\n*** End of Example ***");
  41.         return;
  42.     }
  43.  
  44.     Screen("    Setting the database directory...");
  45.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  46.     ChkRslt(rslt, "SetDirectory");
  47.  
  48.     Screen("    Open the %s master table...", szMasterTable);
  49.     rslt = DbiOpenTable(hDb, (pCHAR) szMasterTable, (pCHAR) szTblType,
  50.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  51.                         xltFIELD, FALSE, NULL, &hCurMaster);
  52.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  53.     {
  54.         CloseDbAndExit(&hDb);
  55.         Screen("\r\n*** End of Example ***");
  56.         return;
  57.     }
  58.  
  59.     Screen("    Display the first %lu records of the %s master table...",
  60.            ulNumRecs, szMasterTable);
  61.     DisplayTable(hCurMaster, ulNumRecs);
  62.  
  63.     Screen("\r\n    Open the %s detail table...", szDetailTable);
  64.     rslt = DbiOpenTable(hDb, (pCHAR) szDetailTable, (pCHAR) szTblType,
  65.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  66.                         xltFIELD, FALSE, NULL, &hCurDetail);
  67.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  68.     {
  69.           CloseDbAndExit(&hDb);
  70.           Screen("\r\n*** End of Example ***");
  71.           return;
  72.      }
  73.  
  74.     Screen("    Display the first %lu records of the %s detail table...",
  75.            ulNumRecs, szDetailTable);
  76.     DisplayTable(hCurDetail, ulNumRecs);
  77.  
  78.     Screen("\r\n    Set to the Customer_No index on the detail table...");
  79.     rslt = DbiSwitchToIndex(&hCurDetail, NULL, NULL, 2, FALSE);
  80.     ChkRslt(rslt, "SwitchToIndex");
  81.  
  82.     Screen("    Link the tables...");
  83.  
  84.     // Put the Customer table into link mode.
  85.     rslt = DbiBeginLinkMode(&hCurMaster);
  86.     ChkRslt(rslt, "BeginLinkMode");
  87.  
  88.     // Put the Orders table into link mode.
  89.     rslt = DbiBeginLinkMode(&hCurDetail);
  90.     ChkRslt(rslt, "BeginLinkMode");
  91.  
  92.     // Link the two tables, specifying which fields
  93.     //   will be linked in the last two parameters.
  94.     //   Specifically, link the first field of Customers
  95.     //   to the indexed second field of Orders.
  96.     rslt = DbiLinkDetail(hCurMaster, hCurDetail, 1, uFieldsMaster,
  97.                          uFieldsDetail);
  98.     ChkRslt(rslt, "LinkDetail");
  99.  
  100.     Screen("    Display the %s detail table after the link:",
  101.            szDetailTable);
  102.     Screen("        Note that all Customer numbers correspond to the last"
  103.            " record displayed in\r\n            the master table");
  104.     DisplayTable(hCurDetail, ulNumRecs);
  105.  
  106.     Screen("\r\n    End the link...");
  107.  
  108.     // Unlink the detail table from the master table.
  109.     rslt = DbiUnlinkDetail(hCurDetail);
  110.     ChkRslt(rslt, "EndLinkMode");
  111.  
  112.     // Take the Customer table out of link mode.
  113.     rslt = DbiEndLinkMode(&hCurMaster);
  114.     ChkRslt(rslt, "EndLinkMode");
  115.  
  116.     // Take the Orders table out of link mode.
  117.     rslt = DbiEndLinkMode(&hCurDetail);
  118.     ChkRslt(rslt, "EndLinkMode");
  119.  
  120.     Screen("    Close the tables...");
  121.  
  122.     // Close the master table.
  123.     rslt = DbiCloseCursor(&hCurMaster);
  124.     ChkRslt(rslt, "CloseCursor");
  125.  
  126.     // Close the detail table.
  127.     rslt = DbiCloseCursor(&hCurDetail);
  128.     ChkRslt(rslt, "CloseCursor");
  129.  
  130.     Screen("    Close the database and exit IDAPI...");
  131.     CloseDbAndExit(&hDb);
  132.  
  133.     Screen("\r\n*** End of Example ***");
  134. }
  135.