Clientdataset as a stand-alone database use learning

Source: Internet
Author: User
Tags savepoint

http://blog.csdn.net/waveyang/article/details/34146737

Unit Unit3;

Interface

Uses
Winapi.windows, Winapi.messages, System.sysutils, System.variants, system.classes, Vcl.graphics,
Vcl.controls, Vcl.forms, Vcl.dialogs, Vcl.stdctrls, Vcl.grids, Vcl.dbgrids,
Data.db, Datasnap.dbclient;

Type
TFORM3 = Class (Tform)
Button1:tbutton;
Button2:tbutton;
Datasource1:tdatasource;
Clientdataset1:tclientdataset;
Dbgrid1:tdbgrid;
Clientdataset1id:tintegerfield;
Clientdataset1qishu:tintegerfield;
Clientdataset1kaijiang:tintegerfield;
Clientdataset1shijian:tstringfield;
Clientdataset1beizhu:tstringfield;
Button3:tbutton;
Procedure Button1Click (Sender:tobject);
Procedure Button2click (Sender:tobject);
Procedure Button3click (Sender:tobject);
Private
{Private declarations}
Public
{Public declarations}
End

Var
FORM3:TFORM3;

Implementation

{$R *.DFM}

Procedure Tform3.button1click (Sender:tobject);
Var
path:string;
Begin
Path: = Extractfilepath (Application.exename); Get the executable file path
Self. Clientdataset1.filename: = Path + ' Test.cds ';
Self. Clientdataset1.loadfromfile (Path + ' Test.cds ');
Self. Clientdataset1.open;

End

Procedure Tform3.button2click (Sender:tobject);
Begin
Self. Clientdataset1.insertrecord ([111,2,3, ' aaaaa ', ' CCCC ']);
End

Procedure Tform3.button3click (Sender:tobject);
var a,b,c:string; I,j,k:integer;
Begin
Self. Clientdataset1.first;
I:=self. clientdataset1.fieldvalues[' id '];

ShowMessage (IntToStr (i));
End

End.

 Summary of the usage of Clientdataset in Delphi2014-06-24 20:48 2081 People read comments (0) favorite reports Summary of the usage of Clientdataset in Delphi Blog Category:
    • Delphi

The TClientDataSet control inherits from Tdataset, whose data store file format has a. cds extension, and is a control based on file-type data storage and manipulation. The control encapsulates the interfaces and functions that manipulate the data, and does not rely on the above-mentioned database drivers, basically satisfying the needs of a stand-alone "thin" database application.

I. Introduction to the basic properties and methods of TClientDataSet

1. fielddefs: Field Definition List Properties

The developer can edit the field by clicking the Property Edit button in the property editor, or by right-clicking the Fields Editor menu in the pop-up menu on the control. When this property is set, it is actually the equivalent of defining the structure of the table, and if you want to load the structure and data of an existing data table, you can select the Assign Local Data menu in the pop-up menu by right-clicking From the Pop-up dialog box, select the dataset control name that is already connected to the database in the current form (the data set control that you want to apply must already be placed in the current form and the activation is turned on).

Use Note:

For a custom field name table, the control still cannot be opened when the property is finished editing. You must right-click the control and select the Create DataSet menu in the pop-up menu so that the control can be activated to open and use after the dataset is created, based on the list of fields edited above. Otherwise, a similar "clientdataset1:missing data provider or data packet will appear." Errors (including during run time, the runtime can invoke the control's CreateDataSet method to dynamically define fields and tables).

2. FileName Property

Description: The name of the data store file. Because the control is a file-based data manipulation control, you must specify the data file name you are manipulating (the default extension name. CDs) to open and activate the control for data editing.

Example 1: Use this property to open the specified. cds file

Delphi Code
    1. Var
    2. path:string;
    3. Begin
    4. Path: = Extractfilepath (Application.exename); Get the executable file path
    5. Cdataset1.filename: = Path + ' Test.cds ';
    6. Cdataset1.open;
    7. End

3. CreateDataSet method

Description: This method uses the field name table in Fielddefs to create a dataset, which is commonly used to dynamically define tables.

Example 2: Dynamically creating a DataSet with a name and age of two fields.

Delphi Code
    1. Create a Field Name table
    2. CDataSet.FieldDefs.Clear;
    3. With CDataSet.FieldDefs.AddFieldDef do
    4. Begin
    5. Name: = ' name ';
    6. Size: = 10;
    7. DataType: = ftstring;
    8. End
    9. With CDataSet.FieldDefs.AddFieldDef do
    10. Begin
    11. Name: = ' age ';
    12. DataType: = Ftinteger;
    13. End
    14. Dynamically creating datasets
    15. Cdataset.createdataset;
    16. Activating and opening the dataset
    17. Cdataset.open;

4. Open method

Description: Opens and activates the dataset control for data editing. A. If the filename attribute is specified, the control can be opened and activated directly using the open method, as shown in Example 1. B. If you do not specify the FileName property, you can use the example 2 method to dynamically create and open a dataset to manipulate the data.

5, LoadFromFile and SaveToFile

Description: Loads the table structure and data from the file and stores the data to the file. This method is similar to opening new files and saving features in Word.

Example 3: Storing data from a dataset in a specified file

Delphi Code
    1. Cdataset.savetofile (' C:\Windows\ZHU\Test.cds ');

6, first (to the top), Prior (forward), Next (backward), last (to tail), edit (Edit), Cancel (cancel edit), Post (Save), insert (insert record), Append (add record), delete (delete), Refresh (data refresh) data sets commonly used methods.

Note: When the FileName property is specified, its Post method can store the data in the specified file, similar to its SaveToFile method, and the Post method stores the data only in RAM if no storage file name is specified. Other methods, with general data set controls, are used in a slightly different way.

7. Filter, Filtered: Filter Filter Properties

Description: Used to filter records for a specified condition, using the same general data set control, slightly.

Example 4: Screening for male-sex records in an open data set that has been activated

Delphi Code
    1. Cdataset.close;
    2. Cdataset.filter: = ' gender = '+ ' male ' + ';
    3. cdataset.filtered: = True;
    4. Cdataset.open;

Ii. Considerations for publishing applications that use the TClientDataSet control:

As mentioned earlier, a program that uses the TClientDataSet control does not require any database drivers when it is published, greatly saving the size of the installation file. However, do not forget to publish the program with the Windows System directory Midas.dll (257KB) with the application (running must), otherwise, the program will still not function properly.

By using the TClientDataSet control in Delphi, both the application can be completely detached from the database driver, and the simple and easy-to-use features of the general data set control are realized, which provides a technical method and means for writing "thin" database application.

Third, TClientDataSet in the three-storey structure, the status of TClientDataSet is immeasurable, this article from the following several aspects of her use.

1. Dynamic indexing

Delphi Code
    1. Procedure Tform1.dbgrid1titleclick (Column:tcolumn);
    2. Begin
    3. if (not column. Field is Tblobfield) Then//tblobfield cannot be indexed, binary
    4. Clientdataset1.indexfieldnames:=column. Field.fieldname;
    5. End

2. Implementation of master-slave table in multilayer structure

Set the main table Clientdataset1.packetrecord to 1, all records;

Set from table Clientdataset1.packetrecord to 0, current record.

3, Taggregates use

(1) In field edit, add new field type is aggregates
After setting expression (express test)
Set Active:=true to
Use the Dbedit field for the former
(2) using the Aggergates attribute add design expression test
Call

Delphi Code
    1. ShowMessage (Floattostr (ClientDataSet1.Aggregates.Count));
    2. ShowMessage (clientdataset1.aggregates.items[0].  Value);

4, in a single-tier database do not BDE

Use Clientdataset instead of table, use Clientdataset loadfilename to load CDs instead of table TableName DB or DBF

Reference: The original program modification method:
Add a clientdataset, use right-click Assign locate data
After SaveToFile, then loadfromfile, delete table after
Set the DataSource of the original connection table to Clientdataset
The only thing to note is: To copy Midas.dll to system or current directory

5, three-layer structure of the portfolio implementation method

Simultaneous settings: filename (*.cds) 2.remote server

6, can be assigned to data (from another data set to take a value)

Delphi Code
    1. Clientdataset2.data:=clientdataset1.data;
    2. Clientdataset2.open;

Or

Delphi Code
    1. Clientdataset2.clonecursor (clientdataset1,true);
    2. Clientdataset2.open;

7. Additional data obtained

The client program requests data from the application server. If the Fetchondemand property of TClientDataSet is set to True, the client program automatically retrieves the value of the attached packet, such as the Blob field, or the contents of the nested table, as needed. Otherwise, the client program needs to explicitly call Getnextpacket in order to obtain these additional packets. Clientdataset Packetrecords Sets the number of records to be obtained at one time.

8, Clientdataset and server-side query connection method

(1) SQL content is empty

Delphi Code
    1. Clientdataset1.close;
    2. clientdataset1.commandtext:=edit1.text;//that is SQL content
    3. Clientdataset1.open;

For no application server settings filter such as: Country like ' A% ', filtered=true can implement SQL functionality.

(2) Parameters

SQL for server query as SELECT * FROM animals
Where name like:d D

Then: Client Clientdataset

Delphi Code
  1. Var
  2. Pm:tparam;
  3. Begin
  4. Clientdataset1.close;
  5. clientdataset1.providername:=' DataSetProvider1 ';
  6. Pm:=tparam.create (nil);
  7. Pm.  name:=' DD ';
  8. Pm. datatype:=ftstring;
  9. ClientDataSet1.Params.Clear;
  10. ClientDataSet1.Params.AddParam (PM);
  11. ClientDataSet1.Params.ParamByName (' DD '). Asstring:=edit1.  Text;
  12. Clientdataset1.open;
  13. Pm. Free;
  14. End

9. Update management of data

(1) SavePoint save data status so far, can be restored to this state

Delphi Code
    1. Var
    2. Pp:integer;
    3. Begin
    4. Pp:=clientdataset1.savepoint;
    5. Clientdataset1.edit;
    6. Clientdataset1.fieldbyname (' name '). asstring:=' the proverb ';
    7. Clientdataset1.post;
    8. Table1. Refresh;
    9. End

Recovery point: clientdataset1.savepoint:=pp;

(2) Cancel,revertrecord

Cancels the modification of the current record, only suitable for no post, if post, call Revertrecord.

(3) CancelUpdate

Cancel all modifications to the database

(4) Undolastchange (Boolean), Changecount

Undo the last modification, which can be undone in succession. The argument is true: the cursor is to the recovery; false: The cursor does not move at the current position. Changecount returns the number of times a record has been modified, one record has been modified several times, and only once, but Undolastchange is undone only once.

10, can write the RECNO

For Ttable and tquery the RECNO is read-only, and the recno of TClientDataSet is readable and writable clientdataset1.recno:=5; is set fifth record as the current record.

11. Data Preservation

For table to use post to update data, and CLIENTDATASET1 post only update memory data, to update server data to make Applyupdates (Maxerrors:integer), he has a parameter, is allowed to issue the number of errors,- 1 represents countless times, with Simpleobjectbroker standing at 0, for automatic fault tolerance and load balancing.

    • An explanation of the usage of the STL list lists
    • An analysis of Clientdataset in the next Delphi

Clientdataset as a stand-alone database use learning

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.