Delphi Database Programming Tutorial (Nine)

zhaozj2021-02-16  77

The fifth chapter is behind the data set data .....

The status of the first data

When developing database applications with Delphi and ADO, most of the work is used to learn the help documentation for the data set component. . To create an ADO-based program, Delphi provides several datasets components: Tadotable, TadoQuery, and other components. They are used to obtain, rendepend and modify data of database tables or queries.

In the fifth chapter of this tutorial, we will understand how to presen, browse, and read data by introducing some attributes, events, and methods of the data set components of most interested.

Selection, Setting, Connection, and Get (Pick, Set, Connect and Get)

Since you have arrived in Chapter 5, you should be familiar with the steps you need to create a database form. In Chapter 4 we have manually established a simple data browsing form. This chapter will use it to continue discussions.

So far, we only use one (ADO) dataset component: tadotable. Understanding TadoQuery and Tadodataset (as a data set component) is important to use the same settings for the same method and event.

Sesame openings; Open Sesame; Close Sesame

One of the important features of Delphi database development is that Delphi can process our data while programming. You can recall - in the previous chapter, we use the Active property to open the activity connection with the data when designing.

It is not difficult to understand that the program must first open the data set before starting the data of the table. Delphi has two ways to implement this function. The first is that we have seen, you can set the Active property when design or runtime; the second is that we can call the OPEN method at runtime. For example, add the following code to the list of oncreate event handles to get the data of the ADOTABLE component.

ADOTABLE1.OPEN

Note: Each ADO dataset can access data from its own Connectionstring property or an Adoconnection component (and its Connectionstring). If the ADOTABLE1 component is connected to the Adoconnection1 component (recommended to use this mode), open ADOTABLE will activate the appropriate Adoconnection components. AdoConnection provides two events that will be executed: OnWillConnect and ONCONNECTCOMPLETE.

The Open method sets the Active property to TRUE and activate the connection. When we handle the connection, we can set the Active property to disconnect to false or call the Close method. Usually we all put into the CLOSE method in the onclose event handle of the form:

ADOTABLE1.CLOSE;

Before proceeding, you know that the processing of data sets and properties depends on the understanding of the current state of the data. Simply put, the State attribute of the data set determines what behavior on the data set can occur or does not occur.

How are you doing?

If the data set is closed, the data of the data will be displayed as an Inactive connection. When the connection is closed, no operation, behavior, or method can be implemented. When we open the connection of the data set, the data set is in the default Browse state. You should always understand the status of "your" data. For example, when we connect the data set to DBGRID, the user can see a potential dataset (or recordset), but if you want to change some data, you must set State to Edit. When program processing data, it is important to understand that the data set status is in constant changes. For example, when browsing data in a DBGRID, the user starts editing records, and the status will automatically change to Edit. Of course, when setting the data visualization control (DBGRID, DBEDIT), this is the default behavior when TRUE is set. But how do we get a state? ADOTABLE (any other data set component is not the same) does not have a trigger when the status is changed.

Ok, let's take a look: For each data set component, we usually use a data source component to present the connection of one or more data visual controls. That's it.

Each data source component has an onStateChange event, regardless of the potential data set status, change it. Place the following code in the onStateChange event handle, display the current status of the ADOTABLE1 dataset component with the title of the form:

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 State' end; caption: = 'adotable1 state:' ds; end;

转载请注明原文地址:https://www.9cbs.com/read-24472.html

New Post(0)