FireDAC.TFDTableAdapter.Main Sample

From RAD Studio Code Examples
Jump to: navigation, search

This sample demonstrates how to use the TFDTableAdapter class to provide communication between the application and the database.

Location

You can find the TableAdapter sample project at:

Description

The TableAdapter sample shows you how to use TFDTableAdapter to transfer data from TFDCommand into TFDMemTable. To this end, the sample implements four TFDCommands for executing different SQL commands.

How to Use the Sample

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

Files

File in Delphi Contains

TableAdapter.dproj
TableAdapter.dpr

The project itself.

fTableAdapter.pas
fTableAdapter.fmx

The main form.

Implementation

The goal of this sample is to demonstre how to use TFDTableAdapter to provide communication between the application and the database. To this end, the following main components are configured:

  • Four TFDCommand objects named cmSelect, cmDelete, cmInsert and cmUpdate.
These objects uses TFDConnection to connect to the desired database. Moreover, each command object is used to implement a different SQL query. To this end, each TFDCommand object use its CommandText property to write a SQL command in order to select, update, delete or insert data from/to a database. The CommandText property of each command object is set up at run time on a FormCreate event.
Note: The setup of each CommandText property could have been done at design time using the Object Inspector.
The TFDTableAdapter object is a mediator between each TFDCommand and TFDMemTable. To this end, the sample set up the following properties at design time using the Object Inspector:
  • The SelectCommand property of adOrders is set to cmSelect.
  • The DeleteCommand property of adOrders is set to cmDelete.
  • The InsertCommand property of adOrders is set to cmInsert.
  • The UpdateCommand property of adOrders is set to cmUpdate.
This component retrieves data from database through TFDTableAdapter and the TFDCommand. To this end, the Adapter property of cdsOrders is set to adOrders.
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 dataset and the grid where the dataset is displayed. To this end, the DataSet property of DataSource1 is set to cdsOrders and the DataSource property of DBGrid1 is set to DataSource1. This set up is done at design time using the Object Inspector.

When you run the application a FormCreate event sets up each CommandText property of each command object. 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 property of the cdsOrders table is set to True in order to open the dataset and read data from the database. Then, the sample displays a table using a TDBGrid component. Once DBGrid1 is filled, you can interact with the sample using the TDBNavigator. To this end, the database navigator is linked with the DataSource1 dataset by setting the DataSource property of DBNavigator1 to DataSource1. Therefore, DBNavigator1 allows you to manipulate records when editing or viewing the data.

Uses

See Also

Samples

Personal tools
Assistance