FireDAC.TFDMemTable.MasterDetail Sample
This sample demonstrates how to set up master-detail relationships between datasets using FireDAC.
Contents
Location
You can find the MasterDetail sample project at:
- Start | Programs | Embarcadero RAD Studio Athens | Samples and then navigate to:
Object Pascal\Database\FireDAC\Samples\Comp Layer\TFDMemTable\MasterDetail
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Description
The MasterDetail sample shows you how to set up master-detail relationships between datasets. The sample uses the master-detail relationship to automatically filter a detail dataset based on a current master dataset record. In this sample, the master dataset has "Order" records, and the detail dataset shows only lines for the current order. Moreover, the sample uses the Mode property of TFDFetchOptions to control how the result set records must be fetched into the FireDAC internal data storage.
How to Use the Sample
- Navigate to the location given above and open
CDS_MasterDetail.dproj
. - Press F9 or choose Run > Run.
Files
File in Delphi | Contains |
---|---|
|
The project itself. |
|
The main form. |
Implementation
To set up a master-detail relationship, this sample uses the Object Inspector to configure the following components at design time as follows:
- Two TFDCommand objects named cmOrders and cmOrderDetails.
- Both objects uses TFDConnection to connect to the desired database. Moreover, both TFDCommand objects use their CommandText property to write a SQL command to select data from a database. In order to set up a master-detail relationship, both CommandText properties have to be set up using the Object Inspector at design time as follows:
- The CommandText property of cmOrders is set to
select * from {id Orders}
in order to select "Order" records.- The CommandText property of cmOrders is set to
- The CommandText property of cmOrderDetails is set to
select * from {id Order Details}
in order to select "Order Details" records.- The CommandText property of cmOrderDetails is set to
- Two TFDTableAdapter objects named adOrders and adOrderDetails.
- This component provides communication between the application and the database. The TFDTableAdapter object is a mediator between TFDCommand and TFDMemTable. To this end, the SelectCommand property of adOrders is set to
cmOrders
while the SelectCommand property of adOrderDetails is set tocmOrderDetails
.
- Two TFDMemTable objects named cdsOrders and cdsOrdDetails.
- This component retrieves data from database through TFDTableAdapter and TFDCommand. To this end, the Adapter property of cdsOrders is set to
adOrders
while the Adapter property of cdsOrdDetails is set toadOrderDetails
.
- Two TDataSource objects named dsOrders and dsOrdDetails.
- This component provides an interface between a dataset component and data-aware controls on a form. In this sample, it is used to provide communication between the datasets and the grids where the datasets are displayed. To this end, the following properties are set up at design time using the Object Inspector:
- The DataSet property of dsOrders is set to
cdsOrders
and the DataSource property of DBGrid1 is set todsOrder
.- The DataSet property of dsOrders is set to
- The DataSet property of dsOrdDetails is set to
cdsOrdDetails
and the DataSource property of DBGrid2 is set todsOrdDetails
.- The DataSet property of dsOrdDetails is set to
- Both TFDMemTable objects are linked in a range based master-detail relationship, where cdsOrders is a master dataset and cdsOrdDetails is a detail dataset. To this end, the following properties of cdsOrdDetails are set up at design time using the Object Inspector:
- MasterFields is set to
ORDERID
.- MasterFields is set to
- IndexFieldNames is set to
ORDERID
.- IndexFieldNames is set to
- MasterSource is set to
dsOrders
.- MasterSource is set to
When you run the application, click on the Use Connection Definition combo box and select an option in order to define a connection. When you select an item of the combo box, the Active properties of cdsOrders and cdsOrdDetails are set to True
in order to open the datasets and read data from the database. Then, you will see two tables, one in each TDBGrid. The DBGrid1 is used to display and manipulate records from the master dataset, while the DBGrid2 is used to display and manipulate records from the detail dataset. Once the grids are filled, you can interact with the Fetch On Demand check box and the Apply updates and Cancel updates buttons. These components have events attached to them to do the following:
- The Fetch On Demand check box implements an OnClick event to implement an automatic fetching of records:
- The Cancel updates button uses the CancelUpdates method to remove all records from the change log and restore both the master and the detail dataset rows to their prior editing state.
- The Apply updates button uses the ApplyUpdates method to apply changes for all records in both the master and the detail datasets change log to the database.
Uses
See Also
- FireDAC
- Defining Connection (FireDAC)
- Master-Detail Relationship (M/D)
- Fetching Rows (FireDAC)
- Fetch on Demand