Chapter 5: Behind the Data Set Data...
The status of the first section of data
When developing database applications with Delphi and ADO, most of the work is spent studying the help documentation for the DataSet component. . In order to create an ADO-based program, Delphi provides several data set components: TAdoTable, TAdoQuery and other components. They are used to obtain, present, and modify data from database tables or queries.
In Chapter 5 of this tutorial, we'll get a practical look at how to present, browse, and read data by introducing some of the properties, events, and methods of most of the dataset components of interest.
Pick, set, connect and get
Now that you've reached Chapter 5, you should be familiar with the steps required to create a database form. In Chapter 4 we have manually created a simple data browsing form. This chapter will use it to continue the discussion.
Until now, we have only used one (ADO) dataset component: TAdoTable. It's important to understand that TADOQuery and TADODataSet (as dataset components) use the same settings for the same methods and events.
Open Sesame; Close Sesame (Open Sesame; Close Sesame)
One of the very important features of Delphi database development is that Delphi allows us to process data during program design. You may recall - in the previous chapter, we used the Active property to open an active connection to the data at design time.
It is not difficult to understand that before starting to process the data of the table, the program must first open the data set. Delphi has two ways to achieve this functionality. The first is that as we have seen, the Active property can be set to True at design or runtime; the second is that we can call the Open method at runtime. For example, add the following code to the OnCreate event handler of the form to obtain the data of the ADOTable component.
ADOTable1.Open;
Note: Each ADO data set can access database data through its own ConnectionString property or an ADOConnection component (and its ConnectionString). If the ADOTable1 component is connected to the ADOConnection1 component (this method is recommended), opening ADOTable will activate the corresponding ADOConnection component. ADOConnection provides two events that will be executed: OnWillConnect and OnConnectComplete.
The Open method sets the Active property to True and activates the connection. When we are done with the connection, we can set the Active property to False or call the Close method to disconnect. Usually we put the call to the Close method in the form's OnClose event handler:
ADOTable1.Close;
Before proceeding, it's crucial to know that working with dataset methods and properties relies on knowledge of the current state of the data. Simply put, the State property of a dataset determines what actions can or cannot occur on the dataset at any time.
How are you doing?
If the data set is closed, the State of the data will show an Inactive connection. When the connection is closed, no operations, behaviors, or methods can be performed on the data. When we first open a connection to the dataset, the dataset is in the default Browse state. You should always know the state of "your" data. For example, when we connect a dataset to a DBGrid, the user can see the underlying dataset (or recordset), but if they want to change some data they must set the State to Edit.
When a program processes data, it is important to understand that the state of the data set is constantly changing. For example, when browsing data in DBGrid (Browse state), the user starts editing records, and the state will automatically change to Edit. Of course, this is their default behavior when the AutoEdit property of the data visualization controls (DBGrid, DBEdit) is set to True.
But how do we get the status? ADOTable (nor any other DataSet component) has no triggers to handle state changes.
Well, let's take a look: For each DataSet component, we typically use a DataSource component to render a connection to one or more data visualization controls. That's it.
Each data source component has an OnStateChange event, which is triggered whenever the underlying data set state changes. Place the following code in the OnStateChange event handler to display the current state of the ADOTable1 data set component using the form's title:
PRocedure TForm1.DataSource1StateChange(Sender: TObject);
var ds: string ;
begin
case ADOTable1.State of
dsInactive: ds:='Closed';
dsBrowse: ds:='Browsing';
dsEdit: ds:='Editing';
dsInsert: ds:='New record inserting';
else
ds:='Other states'
end ;
Caption:='ADOTable1 state: ' + ds;
end ;