Delphi - Database components
1. Database components
In Delphi there are two kind of database components :
The data access components take care of the communication between the application and
the table on disk.
The data aware components allows to view and manipulate the data.
2. Data Access components
In this paragraph we'll take a look at the most important data access components.
2.1 The Table component - the connection with the table on disk
The Table component (of type TTable) is one of the most important
Data Access component in Delphi.
The Table component takes care of the connection between the application and the physical
data on disk.
A few important properties are :
For more properties see the Delphi help.
As said before the property Fields is important. This property is inherited by
TTable from TDataSet and is a list with objects of type TField.
This list represent the columns of the table.
This list is automatic created when the table is opened, and is removed again when
the table closes.
For each type of field is a different TField class like TStringField for strings,
TIntegerField for integers etc.
You can work with persistent fields, that means that for each field a type
declaration is added to the form and that the fields list
is not created when a table is opened.
You can do this by double clicking on the TTable component and then adding all
fields to the list. Watch out!, when the structure of the table on disk changes the
declarations on your form doesn't change, so you will have to redo the persistent trick.
On this manner you can also add calculated fields, which are fields that are not in
the physical table but in your program they act like they are in the table.
With the properties Mastersource and Masterfields you can make
tables detail tables, as in a master-detail relationship.
This means that the table which has the mastersource follows that source by
filtering his data on the value of the masterfield.
2.2 The Datasource component
The Datasource component (of type TDatasource) communicates
with the Table component through the fieldslist and makes the data
available for the data aware components. All data aware components do have a
property DataSource which points to the table component through the datasource component.
The property DataSet points to the TTable component.
2.3 The query component
With the query component (of type TQuery) you can use queries
to limit the records of a table.
The resultset of the query can be viewed via a datasource component just as is done
with a table component.
A Query component is more or less nothing else then a Table component, with the
ability of limiting the records with a SQL query.
A few important properties are :
You need the following components to get a working example :
From table to resultset :

2.4 The StoredProc component
With this component (of type TStoredProc) you can execute
procedures and queries on a (remote) server. For example you can use this component
if
you want to execute a query which is saved in Access.
2.5 The database component and Session component
The database component (of type TDatabase)is used for specific
database tasks for the BDE connection like a login procedure or transaction protocol.
Above then you can have a session component (of type TSession)
which allows you to manage the database connections.
2.6 Events of data access components
In this chapter we looked at the components and a few important
properties.Besides the properties are off course the events.
Delphi offers many events for this components, TTable has 28 events!
It would be a bit boring to discuss them here. Look in the Delphi help for all the
information and just try them out.
3. Data Aware components
The data aware components are in the tab Data Controls of Delphi.
With this controls you can view and manipulate the data.
Data aware components have minimal a property DataSource, for example DBGrid and the
DBNavigator.
Components which act on field level also have a property DataField which points to a
specific datafield in the table.
This components always show the values of the current record.
In this paper we looked at the data access components.
The following paper discusses the methods events and more data aware components.
Click here for the next paper in this tutorial : Delphi - database methods and events.