Key points for using tclientdataset

Source: Internet
Author: User
Tags field table savepoint

The tclientdataset control inherits from tdataset, and its data storage file format extension is. CDs, which is based on the file
Data storage and operation controls. This control encapsulates interfaces and functions for data operation and processing, but does not rely on
Depending on the above database drivers, it can basically meet the needs of standalone "thin" database applications.

1. Introduction to basic attributes and methods of tclientdataset

1). fielddefs: attribute of the field definition list

You can click the edit attribute button in the property editor or right-click the control and select the pop-up menu.
In the "fields Editor" menu to edit fields. After this attribute is set, the table structure is defined.
To load the structure and data of an existing data table, right-click and choose "Assign loca" from the shortcut menu.
L data menu, select the name of the dataset control that has been connected to the database in the current form from the pop-up dialog box (when
The dataset control to be applied must be placed in the front form and activated ).

Note:

After editing a custom field name, the control still cannot be opened. You must right-click the control
Select the "Create dataset" menu in the pop-up menu, and create
After the dataset is activated, it can be opened and used. Otherwise, "clientdataset1: Missing Data
Provider or data packet. "error (including at runtime, The createdataset of the control can be called during runtime
To dynamically define fields and tables ).

2). filename attribute

Description: name of the data storage file.

Because this control is a file-based data operation control, you must specify the name of the data file to be operated on (default
Extension name. CDs) to open and activate the control for data editing.

Example 1: use this property to open the specified. CDs File

VaR
Path: string;
Begin
Path: = extractfilepath (application. exename); // obtain the executable file path
Cdataset1.filename: = path + 'test. CDs ';
Cdataset1.open;
End;

3). createdataset Method

Note: This method uses the field table in fielddefs as the structure to create a dataset. It is often used to dynamically define a table.

Example 2: Create a dataset with the name and age fields dynamically.

// Create a field name table
Cdataset. fielddefs. Clear;
With cdataset. fielddefs. addfielddef do
Begin
Name: = 'name ';
Size: = 10;
Datatype: = ftstring;
End;
With cdataset. fielddefs. addfielddef do
Begin
Name: = 'age ';
Datatype: = ftinteger;
End;
// Dynamically create a dataset
Cdataset. createdataset;
// Activate and open the dataset
Cdataset. open;

4). Open Method

Note: open and activate the dataset Control for data editing.

A. If the filename attribute is specified, use the open method to open and activate the control. See example 1.

B. If the filename attribute is not specified, use the example 2 method to dynamically create and open a dataset, and then perform operations on the data.

5). loadfromfile and savetofile

Note: The structure and data of the table are loaded into the file and the data is stored in the file. This method is similar to opening a new
File and save.

Example 3: store data in a specified file

Cdataset. savetofile ('C: \ windows \ Desktop \ test. CDs ');

6). First (to the beginning), prior (forward), next (backward), last (to the end), edit (edit), cancel (take
Delete edit), post (SAVE), insert (insert record), append (add record), delete (delete), refresh
(Data refresh) and other common dataset Methods

Note: When the filename attribute is specified, the POST method can store data in the specified file, similar to its savet
Ofile method; if the file name is not specified, the POST method only stores data in Ram. Other methods
Dataset control usage, omitted.

7). Filter and filtered: Filter filter attributes

Description: used to filter records with specified conditions. The usage is the same as that of the common dataset control.

Example 4: Filter male records in activated data sets

Cdataset. close;
Cdataset. filter: = 'gender = ''' + 'mal' + '''';
Cdataset. Filtered: = true;
Cdataset. open;

2. Notes for publishing applications using the tclientdataset control:

As mentioned above, no database driver is required when a program using the tclientdataset control is released.
Saves the size of the Installation File. However, when releasing a program, do not forget
Publish the application together (required for running). Otherwise, the program still cannot run normally.

Iii. Conclusion

By using the tclientdataset control in Delphi, the application can be completely detached from the database driver.
It also implements the simple and easy-to-use features of conventional dataset controls, providing a technical solution for compiling "thin" database applications
Methods and means.

The above program passed the test under pwindows98 and delphi5.

In the three-tier structure of tclientdataset, the position of tclientdataset is immeasurable.
No, it is very important. This article describes her usage from the following aspects, hoping to help you.

1. Dynamic Indexing
Procedure tform1.dbgrid1titleclick (column: tcolumn );
Begin
If (not column. Field is tblobfield) Then // tblobfield cannot be indexed, binary
Clientdataset1.indexfieldnames: = column. Field. fieldname;
End;

2. Implementation of Master/Slave tables in Multi-Layer Structure

Set the clientdataset1.packetrecord of the master table to-1. All records
Set the clientdataset1.packetrecord of the slave table to 0. The current record

3. Use taggregates
(1) Add the new field type to aggregates in field editing.
Set expression later)
Set active: = true.
Use dbedit's field as the former.
(2) Use the aggergates attribute add to design the expression
Call
Showmessage (floattostr (clientdataset1.aggregates. Count ));
Showmessage (clientdataset1.aggregates. items [0]. value );
 
4. Do not use BDE in a single-layer database
Use clientdataset instead of table, and use loadfilename of clientdataset to load CDs
DB or DBF that replaces tablename of table
The original procedure transformation method:
Add a clientdataset and right-click assign locate data
Savetofile, then loadfromfile, and then delete table
Set the datasource of the original connected table to clientdataset.
The only note is that you need to copy the ". dll" file to the system or the current directory.
5. Implementation of three-layer structure briefcase
Set 1 at the same time: filename (*. CDs) 2. Remote Server
6. You can assign values to data (values from another dataset)
Clientdataset2.data: = clientdataset1.data;
Clientdataset2.open;
Or
Clientdataset2.clonecursor (clientdataset1, true );
Clientdataset2.open;
7. acquire additional data
The client requests data from the application server. If
The fetchondemand attribute is set to true,
The client automatically retrieves additional data packets, such as blob field values or nested table content, as needed.
Otherwise,
The customer program needs to explicitly call getnextpacket to obtain these additional packets.
Set the number of records obtained at a time in packetrecords of clientdataset.
8. Connection method between clientdataset and Server Query
(1) SQL content is blank
Clientdataset1.close;
Clientdataset1.commandtext: = edit1.text; // SQL content
Clientdataset1.open;
If no application server is configured with a filter such as country like 'a %'
Filtered = true to implement SQL Functions
(2) Parameters
For example, if the SQL statement of the Server query is
Select * from animals
Where name like: dd
Then: client clientdataset
VaR
PM: tparam;
Begin
Clientdataset1.close;
Clientdataset1.providername: = 'datasetprovider1 ';
PM: = tparam. Create (NiL );
PM. Name: = 'dd ';
PM. datatype: = ftstring;
Clientdataset1.params. Clear;
Clientdataset1.params. addparam (PM );
Clientdataset1.params. parambyname ('dd'). asstring: = edit1.text;
Clientdataset1.open;
PM. Free;
End;

9. Data update management
(1) The savepoint is in the data status till now and can be restored to this status.
VaR
PP: integer;
Begin
PP: = clientdataset1.savepoint;
Clientdataset1.edit;
Clientdataset1.fieldbyname ('name'). asstring: = 'slang ';
Clientdataset1.post;
Table1.refresh;
End;
Recovery Point
Clientdataset1.savepoint: = pp;
(2) Cancel, revertrecord
Cancels the modification to the current record, only applicable to those without post. If post is called
Revertrecord
(3) cancelupdate
Cancel all changes to the database
(4) undolastchange (Boolean), changecount
The last modification can be canceled consecutively.
True: cursor to recovery
False: The cursor does not move at the current position.
Changecount
However, undolastchange is revoked only once.

10. writable recno
Recno of tTable and tquery is read-only, while recno of tclientdataset is readable and writable.
Clientdataset1.recno: = 5; set the fifth record as the current record
11. Data Storage
Use post to update table data
The clientdataset1 post only updates memory data. To update server data, use
Applyupdates (maxerrors: integer), which has a parameter that allows Error
Times.-1 indicates countless times. It is often set to 0 when simpleobjectbroker is used to achieve automatic fault tolerance and load balancing.

 

Key points for using tclientdataset

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.