FireDAC.TFDTableAdapter.Main Sample
This sample demonstrates how to use the TFDTableAdapter class to provide communication between the application and the database.
Contents
Location
You can find the TableAdapter sample project at:
- Start | Programs | Embarcadero RAD Studio Athens | Samples and then navigate to:
Object Pascal\Database\FireDAC\Samples\Comp Layer\TFDTableAdapter\Main
- 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 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
- Navigate to the location given above and open
TableAdapter.dproj
. - Press F9 or choose Run > Run.
Files
File in Delphi | Contains |
---|---|
|
The project itself. |
|
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.
- A TFDTableAdapter object named adOrders.
- 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
.
- The SelectCommand property of adOrders is set to
- A TFDMemTable object named cdsOrders.
- This component retrieves data from database through TFDTableAdapter and the TFDCommand. To this end, the Adapter property of cdsOrders is set to
adOrders
.
- A TDataSource object named DataSource1.
- 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 toDataSource1
. 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
- FireDAC
- Defining Connection (FireDAC)
- Working with DataSets (FireDAC)
- Working with Commands (FireDAC)
- Navigating and Manipulating Records
Samples
- FireDAC Connection Definition sample
- FireDAC TFDMemTable Main sample