home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Total C++ 2
/
TOTALCTWO.iso
/
borland
/
32snipit.pak
/
LNKCRSR.C
< prev
next >
Wrap
C/C++ Source or Header
|
1997-05-06
|
5KB
|
136 lines
// BDE32 3.x - (C) Copyright 1996 by Borland International
// LnkCrsr.c
#include "snipit.h"
static const char szMasterTable[] = "customer";
static const char szDetailTable[] = "orders";
static const char szTblType[] = szPARADOX;
//=====================================================================
// Function:
// LnkCrsr();
//
// Description:
// Linking cursors establishes a link between two cursors
// such that the detail cursor has its record set limited to
// the set of records matching the linking key values of the
// master cursor.
//=====================================================================
void
LnkCrsr (void)
{
hDBIDb hDb = 0; // Handle to the database
hDBICur hCurMaster = 0; // Handle to the master table
hDBICur hCurDetail = 0; // Handle to the detail table
DBIResult rslt; // Return value from IDAPI functions
UINT32 ulNumRecs = 10; // Number of records to display,
// 0 = all
UINT16 uFieldsMaster[] = {1}; // Link the first field of Customer
UINT16 uFieldsDetail[] = {2}; // To the second field of Orders
// (Which is indexed)
Screen("*** Linked Cursors Example ***\r\n");
BREAK_IN_DEBUGGER();
Screen(" Initializing the engine...");
if (InitAndConnect(&hDb) != DBIERR_NONE)
{
Screen("\r\n*** End of Example ***");
return;
}
Screen(" Setting the database directory...");
rslt = DbiSetDirectory(hDb, (pCHAR)szTblDirectory);
ChkRslt(rslt, "SetDirectory");
Screen(" Open the %s master table...", szMasterTable);
rslt = DbiOpenTable(hDb, (pCHAR)szMasterTable, (pCHAR)szTblType, NULL, NULL,
0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL,
&hCurMaster);
if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
{
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
return;
}
Screen(" Display the first %lu records of the %s master table...",
ulNumRecs, szMasterTable);
DisplayTable(hCurMaster, ulNumRecs);
Screen("\r\n Open the %s detail table...", szDetailTable);
rslt = DbiOpenTable(hDb, (pCHAR)szDetailTable,(pCHAR)szTblType, NULL, NULL,
0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL,
&hCurDetail);
if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
{
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
return;
}
Screen(" Display the first %lu records of the %s detail table...",
ulNumRecs, szDetailTable);
DisplayTable(hCurDetail, ulNumRecs);
Screen("\r\n Set to the Customer_No index on the detail table...");
rslt = DbiSwitchToIndex(&hCurDetail, NULL, NULL, 2, FALSE);
ChkRslt(rslt, "SwitchToIndex");
Screen(" Link the tables...");
// Put the Customer table into link mode.
rslt = DbiBeginLinkMode(&hCurMaster);
ChkRslt(rslt, "BeginLinkMode");
// Put the Orders table into link mode.
rslt = DbiBeginLinkMode(&hCurDetail);
ChkRslt(rslt, "BeginLinkMode");
// Link the two tables, specifying which fields
// will be linked in the last two parameters.
// Specifically, link the first field of Customers
// to the indexed second field of Orders.
rslt = DbiLinkDetail(hCurMaster, hCurDetail, 1, uFieldsMaster,
uFieldsDetail);
ChkRslt(rslt, "LinkDetail");
Screen(" Display the %s detail table after the link:",
szDetailTable);
Screen(" Note that all Customer No's correspond to the last"
" record displayed in\r\n the master table");
DisplayTable(hCurDetail, ulNumRecs);
Screen("\r\n End the link...");
// Unlink the detail table from the master table.
rslt = DbiUnlinkDetail(hCurDetail);
ChkRslt(rslt, "EndLinkMode");
// Take the Customer table out of link mode.
rslt = DbiEndLinkMode(&hCurMaster);
ChkRslt(rslt, "EndLinkMode");
// Take the Orders table out of link mode.
rslt = DbiEndLinkMode(&hCurDetail);
ChkRslt(rslt, "EndLinkMode");
Screen(" Close the tables...");
// Close the Master table.
rslt = DbiCloseCursor(&hCurMaster);
ChkRslt(rslt, "CloseCursor");
// Close the detail table.
rslt = DbiCloseCursor(&hCurDetail);
ChkRslt(rslt, "CloseCursor");
Screen(" Close the database and exit IDAPI...");
CloseDbAndExit(&hDb);
Screen("\r\n*** End of Example ***");
}