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:
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
- Var
- path:string;
- Begin
- Path: = Extractfilepath (Application.exename); Get the executable file path
- Cdataset1.filename: = Path + ' Test.cds ';
- Cdataset1.open;
- 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
- 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 creating datasets
- Cdataset.createdataset;
- Activating and opening the dataset
- 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
- 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
- Cdataset.close;
- Cdataset.filter: = ' gender = '+ ' male ' + ';
- cdataset.filtered: = True;
- 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
- 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 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
- ShowMessage (Floattostr (ClientDataSet1.Aggregates.Count));
- 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
- Clientdataset2.data:=clientdataset1.data;
- Clientdataset2.open;
Or
Delphi Code
- Clientdataset2.clonecursor (clientdataset1,true);
- 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
- Clientdataset1.close;
- clientdataset1.commandtext:=edit1.text;//that is SQL content
- 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
- 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. Update management of data
(1) SavePoint save data status so far, can be restored to this state
Delphi Code
- Var
- Pp:integer;
- Begin
- Pp:=clientdataset1.savepoint;
- Clientdataset1.edit;
- Clientdataset1.fieldbyname (' name '). asstring:=' the proverb ';
- Clientdataset1.post;
- Table1. Refresh;
- 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