FireDAC.TFDMemTable.NestedDataSet Sample

From RAD Studio Code Examples
Jump to: navigation, search

This sample demonstrates how to store data in a nested dataset using FireDAC.

Location

You can find the NestedDataSet sample project at:

  • Start | Programs | Embarcadero RAD Studio Alexandria | Samples and then navigate to:
    • Object Pascal\Database\FireDAC\Samples\Comp Layer\TFDMemTable\NestedDataSet
  • 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 NestedDataSet sample shows you how to set a nested dataset in a dataset field . To this end, the sample uses the NestedDataSet property. Moreover, the sample implements the following functionalities that are attached to three different button events:

  • Copy the dataset data to another TFDMemTable.
  • Save the dataset data to a file.
  • Read the dataset data from a file.

How to Use the Sample

  1. Navigate to the location given above and open NestedDataSet.dproj.
  2. Press F9 or choose Run > Run.

Files

File in Delphi Contains

NestedDataSet.dproj
NestedDataSet.dpr

The project itself.

fNested.pas
fnested.fmx

The main form.

fNestedCopy.pas
fnestedCopy.fmx

The copied datasets form.

Implementation

The goal of this sample is to demonstrate how to set nested datasets in dataset fields. To this end, the sample implements the following steps at run time:

  • The sample uses a TFDMemTable object named FDMemTable1 to implement an in-memory dataset. Therefore, FDMemTable1 manages data in the client memory. The sample defines FDMemTable1 as follows:
    1. The sample uses the AddFieldDef method to add field definitions to FDMemTable1. The sample adds three fields named: f1, f2 and f3. The sample configures the f1 field with the TFieldType set to ftInteger. Moreover, the TFieldType property of f2 is set to ftString and the f3 field is set to ftDataSet, which means that this field contains a nested dataset.
    2. The sample adds two child fields to the f3 and names them as: f31 and f32. Both child fields are configured with the TFieldType set to ftInteger.
  • The sample fills FDMemTable1 using the Append method with 100 records as follows:
    1. f1 is set to integer values from 1 to 100.
    2. f2 is set to string values from qqq1 to qqq100.
    3. f3 is filled by nested datasets, each consisting of two records. The sample uses the NestedDataSet property to store data in each nested dataset.

When you run the application, you see a table displayed in a TDBGrid. The grid is used to display and manipulate records from FDMemTable1. Once the grid is filled, you can interact with the sample. Each of the following buttons implements an OnClick event to do what is described below:

  • The Save to file button uses the SaveToFile method to save the dataset data to an external file for later use by this or other datasets.
  • The Read file button uses the LoadFromFile method to populate the dataset with data stored in an external file. The data is not moved to a database, it is just loaded into a dataset in-memory storage.
Note: Both methods could work with three different file formats: binary, XML and JSON. The cited methods can work with the three file formats adding the following objects to the form: TFDStanStorageXMLLink, TFDStanStorageJSONLink and TFDStanStorageBinLink.
  • The Show copy button copies the datasets from FDMemTable1 to FDMemTable2 and uses the ShowModal property to show the copied dataset on the grid of the fNestedCopy form.
Note: The ShowModal property shows the fNestedCopy form as a modal form, which means that the application cannot continue to run until the modal form is closed.

Uses

See Also

Samples