Chapter 6 What is the four types of standard data set components in Delphi 4, which are TTable, TQuery, TstoredProc and TclientDataSet. These data sets are inherited from a common base class TDataSet, where only TclientDataSet is inherited directly from TDataSet, while TTABLE, TQUERY, TSTOREDPROC's direct superior is TDBDataSet, TDBDataSet's superior is TBDedataset, TBDedataset's superior It is TDataSet. The inheritance relationship between these classes can be represented by Figure 6.1.
Figure 6.1 Inheritance Relationship of Data Set
TDataSet is an abstract base class for all data sets, and most of its properties and methods are virtual or abstract. The so-called virtual method refers to these methods can be derived overloaded. The so-called abstract method refers to these methods only declares, no definitions, derived classes must be defined before they can call these methods, and different derived classes can have different definitions.
Due to the Abstract method in the TDataSet, you cannot create its instance directly, otherwise it will cause an error error.
If you are functioning, TDataSet's properties and methods can be divided into such blocks: open and close dataset, browse, edit data, bookmark management, control connection, access field, recording buffer management, filtering, and events.
6.1 Opening and closing the data set
First open the data set before any operation of the data set. To open the dataset, you can set the Active property to true, for example:
CustTable.Active: = true;
Open functions can also be called, for example: CustQuery.Open;
To turn off the dataset, you can set the Active property to false or call the Close function.
6.2 Status of Data Set
The status of the data set determines the current operation that can be performed on the data set, for example, when the data set has been turned off, its status is DSINACTIVE, which is not accessible to any data of the data set.
6.2.1 State Attribute
The State property is read-only, and the possible values of the state attribute are listed below:
The .dsinActive data set is closed and cannot access its data;
The .dsbrowse dataset has been opened, you can browse the data but cannot modify the data;
.dsedit At this time, you can modify the data;
.dsInsert can insert a new record at this time;
.dssetkey only applies to TTable and TclientDataSet, you can set the range and key values, and you can call the gotoKey function;
.dscalcfields is processing the oncalcfields event, and the value of the non-calculated field cannot be modified.
. dscurvalue internal use;
.DSNewValue internal use;
.dsoldValue internal use;
.dsfilter is performing filtering operation.
When a data set is just open, its State property is set to DSBROWSE, and the value of the State property will automatically change with the application of the application.
To enable the data set to DSBROWSE, DSEDIT, DSISERT, or DSSetKey status, you have to call the appropriate method.
For example, to make the data set CustTable enters the DSInsert state, the program is now as follows:
Procedure TFORM1.INSERTBUTTONCLICK (Sender: TOBJECT);
Begin
CustTable.Insert; {Enter DSINSERT Status}
AddressPromptDialog.showModal;
If dersspromptdialog.modalResult: = mrok boundtable.post; {Restore to DSBROWSE status}
Else
CustTable.cancel; {Restore to DSBROWSE Status}
END;
As can be seen from the above example, some operations automatically turn the data set into a DSBROWSE state, for example, if the POST function is successful, the data set will return to the DSBROWSE state. If the call post is not successful, the data set still maintains the original state. . Calling Cancel also enables the dataset to recover to the DSBROWSE state.
If you set the Active property to false, or call the Close, the dataset will enter the DSINACTIVE state. For example, the following two lines of code are equivalent:
CustTable.Active: = false;
CustTable.Close;
Some states such as DSCalcFields, DSCurValue, DSNewValue, DSoldValue, and DSFilter cannot be controlled by the application, but by the data set itself as needed. For example, when processing the oncalcfields event, automatically enter the DSCalcFields status. When exiting the handle of the ONCALCFIELDS event, the data set automatically restores the original state.
When the status of the dataset changes, the onStateChange event of the TDataSource component is triggered. If the DataSet property of this TDataSource component specifies this data set.
The following details of the data set will be described in detail and how to enter these states.
6.2.2 DSINACTIVE Status
When the data set is closed, it is in the DSINACTIVE state. At this point, any data cannot be accessed.
To make the data set into the DSINACTIVE state, you can set the Active property to false, or call the Close. The BeforeClose event is triggered before the dataset will be turned off. When the data set is just turned off, an AfterClose event will be triggered. If the CLOSE is called when the data set is in a dsedit or DSInsert state, it should be prompted to be approved or canceled in the handle of the BeforeClose event. The program is now as follows:
Procedure CustTable.verifyBeforeclose (DataSet: TDataSet)
Begin
IF (CustTable.State = dsedit) or (CustTable.State = DSINSERT) THEN
Begin
IF messageDLG ('recognition modification?', MTConfirmation, mbyesno, 0) = mryes dam
CustTable.Post;
ElsecustTable.cancel;
END;
END;
6.2.3 DSBROWSE Status
When a data set is just open, the data set is always in the DSBROWSE status. At this time, records in the data set can be displayed, but records cannot be edited and inserted.
The DSBROWSE state can be considered to be the basic state of the dataset, in which a state can be entered. For example, calling the Insert or an Append function will turn the status of the dataset from dsbrowse into dsinsert (of course, this also depends on other factors, such as the value of the CANMODIFY property), calling setKey will make the data set from dsbrowse to DSSetKey state.
TDataSet has two ways to return the dataset to the DSBROWSE state, one is Cancel, which will cancel the currently ongoing editing, insert, search, etc., so that the dataset is returned to the DSBROWSE state. The other is POST, which will try to save the modified data to the data set. If it is successful, the data set will return to the DSBROWSE state. If it is not successful, the data set remains the original state. 6.2.4 Dsedit status
If the application wants to modify the data of the dataset, you must first enter the DSEDIT status. To enter the Dsedit state, you can call Edit. However, calling Edit does not guarantee that you can enter the Dsedit state, which also depends on the value of the canmodify property. If this property returns true, it means that the dataset is readable and written.
For TTable components, if the readOnly property is set to true, the canmodify property will definitely return false. For the TQuery component, if the RequestLive property is set to false, the canmodify attribute will definitely return False.
Even if the data set enters the dsedit state, it does not mean that the user must modify the data, the readOrthly property of the data control must be set to false. In addition, for SQL databases, users can modify data depends on whether the user has no data to modify data.
To return from the DSEDIT status, you can call the Cancel, POST or DELETE functions, where POST and DELETE are not called successful, will still keep the dsedit status.
In the data control, when the user modifies the data, the input focus is removed, it is equivalent to calling the POST function, which will cause the dataset to return to the DSBROWSE state.
6.2.5 DSINSERT Status
If the application is inserted into a new record, you must first enter the DSInsert state. To enter the DSInsert state, you can call the INSERT or APPEND function. However, calling INSERT or APPEND does not guarantee that you can enter the DSInsert state, depending on whether the value of the canmodify property returns True.
Even if the data set enters the DSISERT state, it does not mean that the user can insert records, the readOrthly property of the data control must be set to false. In addition, for the SQL database, users can insert records also depend on the privileges that the user has modified data.
To return to the DSBROWSE status from the DSInsert state, you can call the Cancel, POST, or DELETE function, where the POST is not called successful, and still keeps the DSInsert state.
In the data control, when the user modifies the data, the input focus is removed, it is equivalent to calling the POST, which will cause the dataset to return to the DSBROWSE state.
6.2.6 DSSetKey Status
You can call Locate, and lookup searches for specific records in the data set. For TTable components, you can also call GotoKey, GotoneareAarest, FindKey or Findnearest to search for specific records in the table. Before calling the above method, you must first make the data set into the DSSetKey state. To make the data set into the DSSetKey status, you can call SetKey. After calling the above method, the data set returns to the DSBROWSE state.
In addition, the data set can be filtered. For TTable components, you can set the range in advance. Before the filtering and range operation, you must first enter the DSSetKey status.
6.2.7 DSCALCFIELDS Status
When the oncalcfields event is triggered, the data set will enter the DSCalcFields state. In handle the handle of the ONCALCFIELDS event, the value of the "calculation field" should be given. In the DSCalcFields state, in addition to the "calculation field", the application cannot modify the value of other fields, because if the value of other fields changes, the oncalcfields event will be triggered, resulting in an infinite loop.
When the oncalcfields event is completed, the data set returns to the DSBROWSE state.
6.2.8 DSFilter status
When the ONFILTERRECORD event is triggered, the data set will enter the DSFilter state. In this state, the record of the data set is not allowed, otherwise, filtering cannot be performed correctly.
When the ONFILTERRECORD event is completed, the data set returns to the DSBROWSE state.
6.2.9 DsNewValue, DSOLDVALUE or DSCURVALUE
When the cached update is allowed, the data set may enter the DSNewValue, DSoldValue or DSCurValue status when the user modifies the record. In these three states, the value at the time can be accessed by the NEWVALUE, OLDVALUE, or CURVALUE attribute of the field (TField).
The above three states are used inside Delphi 4, and the application cannot actively enter the above three states.
6.3 View Record
Each activity data set has a pointer, pointing to the current record. Many of the operations of the data set are for the current record, and many data controls only show the current recorded data, so I know that the location of the current record is very important in the database application.
Database applications tend to change the location of the current record, this time you need to use these methods:
.First causes the first record to become the current record;
.Last makes the last record become the current record;
.Next makes the next record as the current record;
.Prior makes the previous record become the current record;
.Moveby makes a record of the current record a few rows of rows.
In addition, there is a TDBNAVigator component specifically used to browse records, which is implemented by the above method.
In addition to the above method, there are two read-only Boolean properties of the TDataSet to determine the location of the current record, one is BOF, if this property returns True, indicating that the start position of the data set is now available. The other is EOF, if this attribute returns true, indicating that it has now reached the end of the data set.
6.3.1 First and LAST
Calling the FIRST function enables the first record of the dataset to be the current record and set the BOF attribute to TRUE. If the first record is already currently recorded, the first is nothing. The program is now as follows:
CustTable.first;
Calling the Last function enables the last record of the dataset to the current record and set the EOF attribute to True. If the last record is already the current record, Last is not dry. The program is now as follows:
CustTable.last;
There are two buttons on the navigator implemented with TDBNAVigator components, respectively, respectively, respectively, respectively.
6.3.2 NEXT and PRIOR
Calling the next function enables the next record to become the current record. If the current record is already the last record of the dataset, NEXT does not do anything. The program is now as follows:
CustTable.Next;
Calling Prior can make the previous record becomes the current record. If the current record is already the first record of the dataset, the prior is nothing. The program is now as follows:
CustTable.Prior;
6.3.3 Moveby
Call the Moveby function Make another record in the data set into the current record, which is a few rows from the original current record. Moveby needs to pass a parameter, the number of rows separated, and the positive number indicates the direction in which the recording number is increased, and the negative number is moved to the direction of the recording number. Sample example is as follows: CustTable.Moveby (-2);
Moveby returns the number of rows that actually moves, and the return value may be different from the parameters passing to Moveby.
Note: In multi-user environments, other users may be modified, inserted or delete records, so that a record is 5 lines from the current record, and now it is possible to become 4 lines and 6 lines, and even the record does not exist. Because other users modify the data of the record or delete the record.
6.3.4 EOF and BOF Attributes
TDataSet has two read-only attributes EOF and BOF, which are used to determine if the end of the data set is at the beginning. These two properties are often used when all records of the data set.
If the EOF attribute returns true, it is indicated that the end of the data set is now.
Set the EOF attribute to true when performing the following.
Open an empty dataset;
Calling Last;
Call Next, and now the last record of the data set;
Call SetRange, and the scope is invalid.
In addition to the above, the EOF attribute will return false.
The EOF property is usually used in a loop, and each call is required to determine the EOF attribute to avoid operation of the absence of records. The program is now as follows:
CustTable.disableControls;
Try
CustTable.first;
While Not CustTable.eof DO
Begin
...
CustTable.Next;
END;
Finally
CustTable.enableControls;
END;
The above code also demonstrates how to temporarily disable the data control to follow refreshes during all the records of the data set. DISABLECONTROLS should be called for disabled before all records traversed in the data set, which can speed up the speed of traversal, because the refresh is also spent. After the traversal is over, you should call EnableControls to recover refresh.
EnableControls is best called in the Finally part of the Try ... finally structure, which ensures that the refresh can be recovered even if there is an abnormality in the past.
If the BOF attribute returns true, it means that it has now reached the beginning of the data set.
Set the BOF attribute to true when performing the following.
Open a non-empty data set;
Calling first;
Call the PRIOR, and now the first record of the data set.
In addition to the above, the BOF attribute will return false. Like the EOF attribute, the BOF attribute is usually used in a loop, and each time the prior is to determine the BOF attribute to avoid operation of the absence of records. The program is now as follows:
CustTable.disableControls;
Try
While Not CustTable.Bof Do
Begin
...
CustTable.Prior;
END;
Finally
CustTable.enableControls;
END;
6.4 bookmark
The role of the bookmark is to make a mark in a location in the data set, and you can quickly and easily return to that position. Several attributes and methods are provided in TDataSet to manage bookmarks.
If you read the Bookmark property, return to the current recorded bookmark. If you write the Bookmark property, it enables a specified bookmark as the current bookmark.
Several functions about bookmarks in tdatanet are virtual, and TDataSet derived TBDedataset redefines these methods, including:
.Bookmarkvalid determines whether a bookmark is legal; .comparebookmarks compares whether the two bookmarks are the same;
.Getbookmark creates a bookmark to mark the current record;
.Gotobookmark returns to the location of the GetBookmark mark;
.Freebookmark deletes a bookmark.
To create a bookmark, first to declare a TBOOKMARK type variable, then call the GetBookmark function to create a bookmark that tag the current record. The TBOOKMARK type variable is actually a non-type pointer.
Before calling GotobookMark, it is best to call BookMarkValid to determine if the bookmark is legal, because the bookmark mark records may have been deleted. If BookmarkValid returns true, the manual check is legal, you can call the gotobookmark to jump to the location of the bookmark tag.
You can call CompareBookmarks to compare whether the two bookmarks are the same, if the two bookmarks are different, this function returns 1. If both bookmarks are the same or NIL, this function returns 0.
GotoBookmark needs to pass a parameter, namely a bookmark.
FreeBookmark is used to delete a bookmark. When a bookmark has been used, it should be deleted in time because the bookmark is also a kind of resource.
The following code demonstrates the usage of bookmarks:
Procedure Dosomething (const twl: ttable) Varbookmark: TBOOKMARK;
Begin
Bookmark: = tbl.getbookmark;
TBL.DisableControls;
Try
TBL.FIRST;
While not tbl.eof do
Begin
...
TBL.NEXT;
END;
Finally
Tbl.gotobookmark (Bookmark);
TBL.EnableControls;
TBL.FreeBookmark (Bookmark);
END;
END;
6.5 Search specific records
You can call Locate and lookup functions to search for specific records in the data set.
Locate is used to locate a specific record in the data set and make the record become the current record. Locate needs to pass three parameters, the first is the keyfields parameter, which is used to specify which field search, the second is the keyValues parameter, used to specify the corresponding value of each field, the third is Options parameter, used for setting Search options.
The following example search for the value of the company field is "Professional Ltd." record:
VAR
LocatesUccess: boolean;
Searchoptions: TlocateOptions;
Begin
Searchoptions: = [loPartialKey];
LocateSuccess: = CustTable.locate ('Company', 'Professional Ltd.', SearchOptions);
END;
If LOCATE finds a qualified record, turn the record to the current record and return true. If LOCATE does not find a matching record, return false.
For LOCATE, the more fields specified by the Keyfields parameter, the more accurate the searches. If the keyfields parameter needs to specify a plurality of fields, with a semicolon from each other. Because the data type of the field may vary, KeyValues is a Variant type parameter. If the keyfields parameter specifies a plurality of fields, the KeyVALUES parameter must be an array of variable types. The program is now as follows:
With CustTable DO
LOCATE ('Company; Contact; Phone, Vararrayof ([' right Diver ',' P ']), lookartialkey is very similar to Locate, and is also searching specific records in the data set. Different, if a matching record is found, the Lookup returns the value of several fields in the record.
Lookup needs to pass three parameters, the first is the keyfields parameter, which is used to specify which field search, the second is the keyVALUES parameter, used to specify the corresponding value of each field, the third is the ResultFields parameter, used to specify Which fields to return.
The following example searches for the value of "Professional Ltd." in CustTable, and returns the value of the Group, Contact, Phone and other fields:
VAR
LookupResults: Variant;
Begin
With CustTable DO
LookupResults: = Lookup ('Company', 'Professional Divers, Ltd. ",' Company; Contact; Phone ');
END;
If the ResultFields parameter specifies a plurality of fields, Lookup returns an array of variable types. If you do not find a match, lookup will return an empty array. The program is now as follows:
VAR
LookupResults: Variant;
Begin
With CustTable DO
LookupResults: = Lookup ('Company; City', Vararrayof (['right Diver',
'Christiansted']), 'Company; Addr1; Addr2; State; Zip';
END;
6.6 filtering
An application is often only interested in part of the data set, for example, may only be interested in customers from Guangdong in a client table. In this case, filtering techniques can be filtered out with a record of specific conditions.
However, for a lot of data sets, it is best to use queries.
6.6.1 Allow filtering
To filter the dataset, first specify the filter condition, and set the FilterOptions property to set the option (optional), and then set the filter attribute to true. If you don't want to filter, just set the filter attribute to false.
There are two ways to specify filtering conditions: First, set the filter attribute, the second is to give the filter condition in the handle of the ONFILTERRECORD event.
The Filter property is suitable for use in the runtime, which is capable of dynamically specifying filtering conditions, and can change filter conditions as needed. However, the Filter property is a string, and the filtering conditions are relatively simple. Although the operator can form a composite conditional expression, it is limited to several common operators. More importantly, only other field names and constants in the data set are specified by the Filter property, and other data cannot occur.
The ONFILTERRECORD event is much flexible, it can specify a filtration condition during the design period. In the handle of the ONFILTERRECORD event, filter conditions can be arbitrarily specified, and filter conditions can be complicated.
6.6.2 FILTER attribute
The Filter property is a string that sets the Filter property like this:
DataSet1.filter: = '' '' '' '; can also set the filter attribute:
DataSet1.filter: = edit1.text;
The above line code allows users to enter the filtering criteria. Even the two lines of code can be combined:
DataSet1.filter: = '' 'state' '=' edit1.text;
After setting the filter condition, simply set the Filtered property to true, filtering is valid.
Composite filtration conditions can be constructed with comparison and logical operators, including:
"Less;
.> Greater than;
.> = Greater than or equal;
<= Less than or equal;
= Equal to;
<> Is not equal;
The expression in both sides must be True;
.NOT expressions cannot be true;
Two expressions As long as there is a TRUE.
The following example limits the CUSTNO field with the AND operator to be greater than 1400 and less than 1500:
(Custno> 1400) and (Custno <1500);
Note: In the case where the Filtered property is set to True, the user modifies, the inserted record may not meet the filtering conditions, at this time, it will refuse to accept the contradiction with the filter condition.
6.6.3 ONFILTERRECORD event
In the case where the Filtered property is set to True, each record in the data set triggers the ONFILTERRECORD event so that there is a chance to decide whether to filter the record.
Processing the handle of the ONFILTERRecord event with a Boolean Accept parameter, set this parameter to true to accept this record, set this parameter to false to indicate this record. The program is now as follows:
Procedure TFORM1.TABLE1FILTERRECORD (DataSet: TDataSet;
VAR Accept: boolean;
Begin
Accept: = Dataset ['State'] = 'CA';
END;
The above example means that only the value of the State field is the record of CA.
Note: Since each record of the data set triggers the ONFILTERRECORD event, the code to process the ONFILTERRECORD event should be as short as possible, especially for a large data set with many records.
Sometimes, the program needs to filter in a variety of different filtration conditions, can establish a plurality of handles that handle the ONFILTERRECORD event, and then dynamically switch the event handle during the running period, the program example is as follows:
DataSet1.onfilterRecord: = newyorkfilter; refresh;
6.6.4 Setting the filter option
The FilterOptions property is used to set the filtering options. This attribute is a collection, which can be empty (default) or the following elements:
.focaseinsensitive compares the string ignore the case;
. FopArtialCompare must match all the words for string types, and partially match is not allowed.
For example, in order to ignore the case when the STATE field is compared, it can be set:
FilterOptions: = [FocaseInSensitive];
Filter: = '' 'state' '=' 'ca' ''; 6.6.5 Browse record after filtered data
The filtered data set is actually a subset of the original data set. TDataSet provides four ways to browse records in filtered data sets, they are:
.FINDFIRST makes the first record in the filtered data set becomes the current record;
.Findlast makes the last record in the filtered data set becomes the current record;
.FindNext makes the next record in the filtered data set as the current record;
.Findprior makes the previous record in the filtered data set becomes the current record.
The above four methods If the call is successful, return TRUE, otherwise, return false. You can check a read-only Found property to see if the last call is successful.
If the filter condition is set via the Filter property or the ONFilterRecord event, the Filtered property is set to false, and the above four methods are called automatically, and the filter is automatically allowed, and then the location of the current record is moved, and finally the filtering is prohibited. In other words, the above four methods can ignore how the Filtered property is set.
If the filter condition is not set, the above four methods are equivalent to First, Last, Next, and Prior.
6.7 repair data
Some methods are available in TDataSet to update, insert, and delete records in data concentration, which are:
.Edit makes the data set into the Dsedit status;
.Append Add a record at the end of the data set;
.INSERT inserts a record in the current location of the data set;
.Post tries to write the user to the data set;
.CANCEL cancels the user's modification of the data, so that the dataset is returned to the DSBROWSE status;
.Delete deletes the current record.
6.7.1 Enter dsedit status
To edit the record of the data set, first go to the Dsedit status. To enter the Dsedit state, call the Edit function. However, calling Edit does not necessarily enable the data set into the DSEDIT state, but also on the value of the canmodify attribute.
Once the data set enters the DSEDIT state, the user can modify the current record on the data control. When the user removes the input focus from the current record, it is equivalent to calling the POST function. The program is now as follows:
With CustTable DO
Begin
EDIT;
FieldValues ['Custno']: = 1234;
POST;
END;
To cancel the currently unresser modification, the user can press the ESC button or click the CANCEL button on the navigator implemented with the TDBnavigator component.
When using the cache update technology (CacheDuPdates property is set to true), calling POST just writes the data to the cache, not directly written to the data set. To write the data in the cache to a data set, you need to call the ApplyUpdates function.
6.7.2 Insert a new record
To insert a new record in the data set, first go to the DSInsert state. To enter the DSInsert state, you can call the INSERT or APPEND function. However, calling INSERT or APPEND does not necessarily enable the data set into the DSIrt state, but also on the value of the canmodify property.
Once the DSInsert state is entered, the user can insert a new record in the data control (typically TDBGRID) and record this record.
If you want to insert a new record by programming, you should pay attention to the difference between INSERT and APPEND. INSERT will insert a new record into the front of the current record, and Append will add a new record to the end of the data set.
After inserting a new record, call POST or call ApplyUpdates to write new records to the data set in the case where the cachedupdates property is set to TRUE. If the data set is a PARADOX or DBASE table that has established an index, the new record will be automatically moved to the appropriate location.
If the data set does not establish an index, the new record is inserted into the current location of the data set (INSERT) or the end (Append).
6.7.3 Delete Record
Call the Delete function will delete the current record and make the dataset back to the DSBROWSE state. If there is a TDBNAVigator component on the form, the user can click the "Delete" button on the navigator to delete the current record. After the current record is deleted, the next record is the current record.
If the deleted is the last record, the previous record becomes the current record.
6.7.4 Modifying the entire record
In addition to TDBGRID and TDBNAVigator, most of the data controls can only work on one or several fields of the dataset, not the entire record.
However, TDataSet provides several ways to directly modify the entire record instead of separate fields, including:
.AppendRecord is similar to the append, but can assign a value to the field, and do not need to call the POST;
. INSERTRECORD is similar to INSERT, but can assign a value to the field, and do not need to call POST;
.SetFields assigns a value for the current record, need to be explicitly calling the POST.
All of the three methods described above are transmitted to an array of TvarRec types as a parameter, and each element of the array corresponds to a value of a field. If the array is less than the number of fields of the data set, the value of the remaining field is NULL.
For data sets that have not established indexes, AppendRecord adds a new record to the end of the data set. For data sets that have established indexes, new records will be automatically moved to an appropriate location.
SetFields is used to assign a value for the current recorded field. Before calling setfields, you first call Edit to enable the dataset into the Dsedit state. After calling setfields, you need to explicitly call the POST function.
When you call SetFields, if you only want to assign a value for some fields, let the value of other fields remain unchanged, you can use null or nil to assign a value.
Suppose a data set has five fields, named, Capital, Continent, Area, and Populations, which can be assigned to them:
CountryTable.InsertRecord (['japan', 'tokyo', 'asia']);
The above program is inserted into a new record in the data set, and the first three fields are assigned. It is now possible to assign the current record again, but this time I only want to assign a value for the Area field and the population field, the program will write:
With countrytable do
Begin
If Locate ('Name', 'JAPAN', LOCASEINSENSITIVE) THEN
Begin
EDIT;
SetFields (NIL, NIL, NIL, 344567, 164700000);
POST;
END;
END;
Note: Use nil instead of null here, otherwise, the first three fields will be set to empty.
6.8 incident
TDataSet's events are divided into two categories. One is the Before series, and the other is the After series, the list is as follows:
.Beforeopen, afteropen happens before and after the data set is opened;
.Beforeclose, AfterClose happens before and after the data set is turned off;
.BeForeInsert, AfterInsert takes place before and after inserting a new record; .beforeeEdit, Afteit occurs before and after entering the Dsedit state;
.Beforepost, afterpost occurs before and after the writing data set;
.Beforecancel, Aftercancel happens before and after the modification;
.BeforeDelete, Afterdelete occurs before and after the removal record.
In addition, the ONNEWRECORD event is triggered when a new record is added to the data set, and the oncalcfields event will be triggered when the value of the "calculating field" needs to be returned.
The BEFORE series is often used to abort the operation. For example, when the calling the Delete function attempts to delete the current record, the BeforeDelete event will be triggered before the current record will be deleted, and you can call Abort or trigger an exception to delete the current record in the handle of the BeforeDelete event.
Pocedure TFORM1.TABLEBEFOREDELETE (DataSet: TDataSet)
Begin
IF Messagedlg ('delete this record?', Mtconfirmation, mbyesnocancel, 0) <> mryes dam
END;
The event of the After series is often used to notify the user on the status bar, and the program is as follows:
Procedure TFORM1.TABLE1AFTERDELETE (DataSet: TDataSet);
Begin
Statusbar1.simpletext: = format ('has% D record', [Dataset.RecordCount]);
END;
The oncalcfields event is mainly used to give the value of the "calculation field". The value of the AutoCalcFields property determines when the oncalcfields event will occur.
If the AutoCalcFields property is set to True, an oncalcfields event occurs in the following cases:
The data set is turned on;
In the data control, the input focus moves from one record to another record;
In the data control, the input focus is moved from a field to another;
The current record is modified or recorded from the database.
However, even if the AutoCalcFields property is set to False, the oncalcfields event is triggered when the value of any of the non-calculated fields in the data set changes.
Since the oncalcfields event is likely to occur frequently, the code to process the oncalcfields event is as short as possible. In the case where the AutoCalcFields property is set to TRUE, the data of the data set cannot be modified in the handle of the ONCALCFIELDS event, because once the current record is modified, the oncalcfields event is also triggered, resulting in an infinite loop. For example, suppose you call POST in the handle of the oncalcfields event, you will trigger the oncalcfields event, resulting in calling POST again, triggering the oncalcfields event again ...
6.9 tbdedataset
TBDedataset is inherited from TDataSet, which provides the ability to access data through BDE (BORLANDDATABASE Engine). This section mainly introduces TBDedataset, readers should have a more profound understanding of TDataSet described earlier.
Like TDataSet, TBDedataset is also virtual and abstract unless you want to create a custom dataset, otherwise, it is generally not required to use TBDedataset.
TBDedataset recommends the method of recording navigation, indexing, and bookmarks in TDataSet, adds some properties, methods, and events that process blob fields, cache updates. 6.9.1 Cacheblobs Attributes
The Cacheblobs property of the TBDedataset is used to control whether the BDE is placing the content of the BLOB field into the cache. If this property is set to true, when the application reads the value of the blob field, BDE will put the contents of the BLOB field in the cache, so that when the application reads the value of this field next time, no need to The database server retrieves, as long as it is taken directly from the memory, this can improve the performance of the application.
However, if the application needs to update the value of the blob field frequently, then the cacheblobs property should be set to false so that the value of the retrieved BLOB field is always the latest.
6.9.2 Cache Update
TBDedataset provides technology for cache updates. The so-called cache update is that the application retrieves data from the database, establish a copy in the local cache, and the user is modified to the data, it is only reflected in the cache, and Applyupdates can be called all modifications to the data set in the data set. .
It can be seen that the cache update technology can significantly improve the performance of the application, and can easily cancel the modification, as long as Applyupdates have not been called. The properties, methods, and events of cache updates are listed below:
.Cachedupdates If this property is set to true, the cache update is valid;
.UpdateObject is used to specify a TUPDATESQL component to update the query-based data set;
.Updatepending If there is a recorded record in the cache, this property returns True;
.UpdateRecordtypes specifies which records in the data set are visible;
.UpdateStatus returns the current update status;
.OnupdateError If the error will trigger this event during the update process;
.OnupdateRecord, each update, a record, will trigger this event;
.Appilyupdates write data in the cache to the data set;
.Cancelupdates Cancel unrescribed changes in the cache;
.CommitUpdates turn off the cache; l Fetchall Retrieves all records from the database to the cache;
.RevertRecord revokes the changes to the current record.
6.10 TDBDataSet
TDBDataSet is inherited from TBDedataSet, which provides the ability of database and session management.
Several properties and methods have been added to manage databases and BDE sessions, including:
.CHECKOPEN Checks if the database is turned on;
.DATABASE returns a TDATABASE component;
.DBHANDLE returns a BDE handle and use this handle when calling the API of the BDE;
.Dblocale returns the current international language driver;
.Dbsession returns a BDE session target;
.DATABASENAME is used to specify the database to be accessed;
.SessionName is used to specify a BDE session period.
Here, explain the DatabaseName property and the sessionname property. If the application is to access the remote database server such as Sybase, Oracle or InterBase, you should use the TDatabase component to connect to the database. At this time, you should set the databaseName property to specify the database to be connected, which can be set to the name of the TDATABASE component. If there is no explicit use of the TDATABASE component, the DatabaseName property should be set to the BDE alias. For Paradox and DBase tables, you can set the path to the table.
The sessionName property is used to specify a BDE session period. If the application does not explicitly use the TSession component, this property is not required. If the application explicitly uses multiple TSESSION components, you should set one of the sessionname properties to specify one. In general, the application does not use attributes such as DBHANDE, DBLOCALE, and DBSESSION unless the API of the BDE is called directly. These three properties are read-only.
There is also a read-only provider property in the TDBDataSet, which can return an iProvider interface. In multi-layered Client / Server applications, the client needs to communicate with the application server via the iProvider interface.