HowToUseDataAwareControls topic
HowToUseDataAwareControls - Hints for accessing databases.Declaration
HowToUseDataAwareControlsDescription
HowToUseDataAwareControls - Hints for accessing databases. A set
of Data-Aware components is provided, to make it easier to access information
held in databases.
Before using Data-Aware components, it is necessary to make a connection with
a database, and this should be done either using a TSQLConnection or a TDbf
component, depending on the type of database that is being used. In any case,
the components for connection with the database need to be found in
$Lazdir/components/sqldb/, $Lazdir/components/tdbf/ or whatever other source
is appropriate. They need to be added using the Components Menu
Item of the IDE, and compiled ready for adding. The IDE needs to be re-built
to include the new components.
To make the connection with the database: first place a DataBase
Connection on the Form (invisible at run-time, so put it anywhere
convenient); set its properties to connect the user (with name and password)
to the appropriate server, database and table (the connection component
should deal with all the user-authentication issues, opening and closing the
database etc). Then put on a TDataSource from the Data Access tab
of the Component Palette, and perhaps items such as
TSQLTransaction and TSQLQuery, depending on the
particular database you are using. The various database components need to be
linked to each other, for example by setting the Transaction
property of a database connector to the appropriate transaction component,
and linking the DataBase property of the Transaction component
back to the database connector; link the DataSource's DataSet
property to the appropriate SQLQuery or other set of data, and its
Transaction property to the transaction component.
Each data-aware control (on the Data Controls tab of the Component Palette)
has the usual set of properties to determine its shape and size, position,
color and font, etc. But there are also special properties to define the data
to be displayed or modified. These include the DataSource property
which must be set to link to the defined set of data, and often a
Field component to determine which of the columns in the database
is to be displayed.
Most of the components operate on a single Row or record of the database, but
the TDBGrid is capable of displaying and operating on multiple
rows and columns, in a StringGrid format, allowing the user to get
an overview of what is in the database. The DBGrid will display the contents
of the DataSet even at design time, provided the various components are
enabled or rendered active; this allows the designer to ensure that all the
connections to the database have been correctly made.
The TDBNavigator component is used in conjunction with the
Data-Aware components. It consists of a series of buttons to allow browsing
through the DataSet, displaying the first, previous, next or last record (or
Row), inserting or deleting records, committing the changes back to the
database or refreshing the DataSet from the database.
The way database programming works is typically as follows:
- a database is opened for access by the user (Database Connection)
- a command is issued (e.g. by
TSQLQuery) to select or extract some or all of the records which are then stored locally in theDataSet - the contents of the
DataSetare acted on by the Data-Aware components, displaying one Row or record at a time from selected Columns, or perhaps the whole DataSet in aDBGrid, editing records, inserting or deleting records, or using the data to perform statistical tests or calculations - at intervals usually prescribed by the user (sometimes after every change, sometimes after a whole series of changes), the contents of the local DataSet are sent back to the database, causing an update of the database's contents. This process is referred to as 'Commit' or 'Post' or sometimes as 'Update'
The programmer can determine which events are associated with a Commit action
by supplying suitable event handlers for OnClick,
OnChange, OnFinishedEdit etc.
The following table shows the approximate correspondence between conventional and data-aware control classes:
| Standard Control | Data-Aware Control | Comment |
| TCalendar | TDBCalendar | Select a date from pop-up calendar |
| TCheckBox | TDBCheckBox | Check a box to show an option has been selected |
| TComboBox | TDBComboBox | Select an option from a list and return it (or some custom text) in the main edit box |
| TEdit | TDBEdit | Single-line text editing box |
| TGroupBox | TDBGroupBox | A container for several related controls |
| TImage | TDBImage | A Picture |
| TListBox | TDBListBox | A list of string items |
| TMemo | TDBMemo | Multiple-line text editing box |
| TRadioGroup | TDBRadioGroup | A group of mutually exclusive Radio Buttons; only one at a time may be checked and selected. Please note that there is no data-aware equivalent of the singular TRadioButton control. |
| TText | TDBText | A simple text-string, usually read-only |
| TStringGrid | TDBGrid | A grid to display information in Rows and Columns |
Standard controls can be used to display information from a database, but they lack the direct link with the DataSet and its navigation aids. The programmer must write explicit code to extract data from the DataSet and put it in the standard control.
An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
