![firedacimagenotitle02 | Embarcadero RAD Studio Delphi C++Builder Blogs](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/06/firedac-image-no-title02.jpg?resize=1140%2C680&ssl=1)
The sample demonstrates how to use the TFDQuery component in order to set up master-detail relationships between datasets. It 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, you can modify in run time the Cache property of TFDFetchOptions to control the data allocation in cache memory.
Table of Contents
Location
You can find the MasterDetail sample project at:
- Start | Programs | Embarcadero RAD Studio Sydney | Samples and then navigate to:
Object PascalDatabaseFireDACSamplesComp LayerTFDQueryMasterDetail
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
![masterdetails | Embarcadero RAD Studio Delphi C++Builder Blogs masterdetails](https://i0.wp.com/blogs.embarcadero.com/wp-content/uploads/2020/11/masterdetails-3070984.png?resize=750%2C409&ssl=1)
How to Use the Sample
- Navigate to the location given above and open
Qry_MasterDetail.dproj
. - Press F9 or choose Run > Run.
Files
File in Delphi | Contains |
---|---|
Qry_MasterDetail.dproj Qry_MasterDetail.dpr | The project itself. |
fQueryMasterDetail.pas fQueryMasterDetail.fmx | 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 TFDQuery objects named qryOrders and qryOrderDetails. These components are used to implement a dataset capable of executing SQL queries. The following setup is needed:
- Configure the Connection property of both components to specify the FireDAC connection object that is used to connect to a DBMS.
In order to set up a master-detail relationship, the SQL properties of both components have to be set up using the Object Inspector at design time as follows:
- The SQL property of qryOrders is set to
select * from {id Orders}
in order to select “Order” records. - The SQL property of qryOrderDetails is set to
select * from {id Order Details} where OrderID = :OrderID
in order to select “Order Details” records.
Finally, both objects are linked in a master-detail relationship, where qryOrders is the master dataset and qryOrderDetails is the detail dataset. To this end, you have to set up the following properties of qryOrderDetails:
- MasterFields is set to
ORDERID
. - IndexFieldNames is set to
ORDERID
. - MasterSource is set to
dsOrders
.
- Two TDataSource objects named dsOrders and dsOrdDetails. These components provide 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
qryOrders
and the DataSource property of DBGrid2 is set todsOrder
. - The DataSet property of dsOrderDetails is set to
qryOrderDetails
and the DataSource property of DBGrid1 is set todsOrderDetails
.
- The Cache details check box includes
fiDetails
into the FetchOptions.Cache property of qryOrderDetails if the check box is checked. It means that if it is checked, the associated detail record will not be discarded from the memory after changing a master dataset record. - The Refresh master button uses the Refresh method to re-fetch the master data from the database.
- The Refresh details button uses the Refresh method to re-fetch the detail data from the database.
Please refer to the link below for more information about the sample:
http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FireDAC.TFDQuery.MasterDetail_Sample
Head over and check out the full source code for the master detail Windows sample app on GitHub.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition