Key Points of developing a single thin database program using Delphi

Source: Internet
Author: User
Tags field table


I. Overview
As a quick development tool for Windows, Delphi can not only develop common Windows ApplicationsProgramAnd has powerful database application development functions. Delphi provides support for BDE, ODBC, ADO, and InterBase Database drivers to meet the needs of different applications for database program development.

However, when publishing database programs developed with Delphi, in addition to installing applications, you also need to publish database drivers at the same time. This is a little tricky for some stand-alone applications that only involve one or more simple table data storage. In addition, some applications themselves need to store a large amount of data, but they require short results, the conventional database development method with Delphi cannot meet the needs.

So is there a way to solve the above conflicts and develop a "thin" Single-host database application that can break away from a large database driver? Delphi5 provides a tclientdataset control in the Midas control panel, which can solve this problem well.

Ii. Usage of tclientdataset

The tclientdataset control inherits from tdataset, and its data storage file format extension is. CDs. It is a control based on file-type data storage and operations. This control encapsulates the interfaces and functions for data operation and processing. It does not rely on the above database drivers and basically meets the needs of single-host "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 choose "fields Editor" from the shortcut menu to edit the field. After this attribute is set, the table structure is defined. If you want to load the structure and data of an existing data table, you can right-click the "Assign local data" menu in the pop-up menu, in the displayed dialog box, select the name of the dataset control that has been connected to the database in the current form (the dataset control to be applied must be placed in the current form and enabled ).

Note:

After editing a custom field name, the control still cannot be opened. You must right-click the control and select the "Create dataset" menu in the pop-up menu to enable the control to be opened and used only after the dataset is created based on the list of fields edited above. Otherwise, an error similar to "clientdataset1: Missing Data Provider or data packet." will occur (including calling the createdataset method of the control during runtime to dynamically define fields and tables ).
2). filename attribute

Description: name of the data storage file.

Because the control is a file-based data operation control, you must specify the name of the operated data file (default extension name. to open and activate the control, and then edit the data.

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 the function of opening new files and saving files in word.

Example 3: store data in a specified file

Cdataset. savetofile ('C: windowsdesktoptest. CDs ');

6 ). first (to the beginning), prior (forward), next (backward), last (to the end), edit (edit), cancel (unedit), post (SAVE ), insert (insert record), append (add record), delete (delete), refresh (data refresh), and other Common Data Set Methods

Note: When the filename attribute is specified, the POST method can store data to the specified file, similar to its savetofile method. If no storage file name is specified, the post method only stores data in Ram. Other methods, similar to the common dataset control usage method, are 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, greatly saving the size of the Installation File. However, when releasing a program, do not forget to publish the software with the application in the Windows System directory, which must be running. Otherwise, the program will still fail to run normally.

Iii. Conclusion

By using the tclientdataset control in Delphi, the application can be completely detached from the database driver, and the conventional dataset control is easy to use, it provides a technical method and means for compiling thin database applications.

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.