Populate Main Dialog with Components (dbExpress Tutorial)
In this example, the dialog's visual components provide several capabilities:
- Choose a database connection
- Choose a table from the database
- View and navigate through database data
- Update database data
- Execute SQL statements against the database
Equally important, the dialog's nonvisual components provide the objects needed to connect to and manipulate databases:
- Database connection
- Datasets for data
- Provider to get data from a dataset and resolve updates to that dataset
- Data source for data-aware components
Here is what the form looks like after we have assembled the components:
The five nonvisual components (the TSQLConnection and the rest) are shown in a column on top of a listbox, but their location on the form does not matter.
Create a new project.
- For Delphi, choose File > New > VCL Form Application - Delphi.
- For C++, choose File > New > VCL Form Application - C++Builder.
Set the form's Caption property to "DB ClientDataSet Data Update Tool". Change the form's Width property to 650 and its Height to 375.
Click File > Save All to save the project.
- For Delphi, save the file as cdsmain.pas. Save the project as DB_CDSDataUpdated.dproj.
- For C++, save the file as cdsmain.cpp. Save the project as DB_CDSDataUpdated.cbproj .
Add Visual Components
From the Tool Palette, drag a TComboBox to the upper left of the form. The application uses this control to list the database connections available. Change the Name property to "ComboBoxConnections". Set AutoDropDown to true. Choose csDropDownList for Style. Set TextHint to "Connection Name" and ShowHint to true.
Drag another TEdit component below the TComboBox. This control displays an expression to filter which database tables are listed in the TListBox below. Change the Name property to "EditFilter". Also, set the Hint property to "Filter (Use * as wildcard)" and the Text property to "Filter (* as wild)" to provide a hint and initial text for this field.
Place a TCheckBox next to the filter TEdit and change the TCheckBox's Caption to "Open". This TCheckBox indicates whether the TClientDataSet set is active or not. Set the TCheckBox's Name property to "CheckBoxActive".
Below the filter TEdit, place a TListBox component. The TListBox lists the tables available in the selected database connection, subject to the filter text in the filter TEdit component above. For the Anchors property, set all the anchors to true except for akRight. This maintains the TListBox's current position relative to an edge of the form, even if the form is resized, except for the right side of the TListBox.
Finally, drag a TDBGrid next to the TListBox. This component displays and manipulates records from a dataset, automatically labeling each table column with the column's database name. We can use the TDBNavigator to move through the TDBGrid. For the Anchors property, set all four anchors to true. This maintains the TDBGrid's current position relative to an edge of the form, even if the form is resized.
Resize all these components appropriately for their location on the form. Save everything.
Add Database Components
Now we add the components to communicate with the database. As we place them on the form, we set their properties to provide the necessary connections between them. Since these are not visual components, their location on the form is a matter of convenience. In this sample, we place them sequentially in a column reflecting the order of data flow.
First, place a TSQLConnection component. This encapsulates a dbExpress connection to a database server. We'll connect this to an actual database connection when the application runs. You can also set the ConnectionName property at design time. The ConnectionName property's drop-down menu shows all the database connections listed in Data Explorer. Set the TSQLConnection's LoginPrompt property to false, so that we are not prompted for a password each time we connect to a database.
Add a TSQLDataSet component, which represents data retrieved using dbExpress. Connect this component to the TSQLConnection component by setting the dataset's SQLConnection property to "SQLConnection1", using the property's drop-down menu. Set DbxCommandType to "Dbx.SQL", since the application uses SQL commands to get data from the database.
Now drop a TDataSetProvider component on the form. This provides data from a dataset and resolves updates to that dataset. The TSQLDataSet from dbExpress is ordinarily a unidirectional dataset. A provider, such as TDataSetProvider, allows us to freely navigate through and update a database. Set the TDataSetProvider's DataSet property to "SQLDataSet1" to connect the provider to the TSQLDataSet.
Place a TClientDataSet on the form to implement a database-independent dataset. Set the TClientDataSet's ProviderName property to "DataSetProvider1", so the TClientDataSet is connected to the TDataSetProvider.
Lastly, put a TDataSource on the form. A data source provides an interface between a dataset component and data-aware controls, such as the TDBGrid used in this example. Set the TDataSource's DataSet property to "ClientDataSet1" using the property's drop-down menu.
To complete the connections between the components, connect the two data-aware visual components to the data source:
- Set the TDBNavigator's DataSource property to "DataSource1".
- Set the TDBGrid's DataSource property to "DataSource1".
Save the file again.
Now that the components are in place, next we can write the application code, using the extensive VCL library of component classes. This tutorial describes the code for both Delphi and C++.