Dynamically Create a table definition of a clientDataSet

zhaozj2021-02-17  45

Foreword

Many people are asking, how CLIENTDATASET can create up with programs without connecting the database, and open the data set.

After studying the TclientDataSet data set, find that if you want ClientDataSet (open), you must meet one of the three conditions:

The ProviderName property is assigned, that is, the data source provider. DATA attribute assignments. Table structure and data are obtained from other open data sets. FileName assigns data and metadata from the local file.

These three conditions are the help of the TclientDataSet's Active property. Think about it, the first and second are basically excluded, if I have a ready-made data set, do you want to create a dry? The third is not so easy, what do you know what the file is formatted?

What should I do?

Failed attempt

Suddenly I remembered the INTERNALCALC (internal calculation) field in TclientDataSet. The internal calculation field can be added in the design period, or can be added dynamically at the runtime. For convenience, we join in the design period. See below:

You can enter the field editor by double-clicking on the control (TclientDataSet), then right-click the "New Field" command to get the interface shown in the figure. Fill in Name, Type and FieldType.

Remember: FieldType must be "InternalCalc". So repeated, you can choose to add multiple fields.

turn on! An error: "ProviderName" or DATA is not assigned.

This method is incorrect. Since I want to Data, I try to assign it from the DATA attribute of other clientDataSet. The result is that the fields of your new fields and data sources are displayed.

I have made further try: That is to assign this of the new field and the original field of the clientDataSet to another new clientDataSet, but found that the new field does not enter.

In general, this attempt is failed, but there is a summary:

Light is not feasible to rely on new internal calculation fields. The newly created calculation field cannot be passed to the second clientDataSet.

Xml

It is an XML! ClientDataSet This is a disconnected dataset control, which provides a function of the function savetofile () that caches the data to the disk, and its detailed declaration is as follows:

Procedure savetofile (const filename: string = ''; format: tdatapacketformat = dfbinary);

Take note of TDataPacketFormat. It is an enumeration type, all of the enumeration values ​​are as follows:

TDataPacketFormat = (Dfbinary, DFXML, DFXMLUTF8);

Everyone may notice that Dfbinary and DFXMLUTF8 are not what we need to care, care about our DFXML format. This is an exciting message. With XML, we can view the definition of the clientDataSet Metadata, and more changes!

Call the SaveTofile function, pay attention to the save format is DFXML. Open the file, as shown below:

- -

-

-

Look at this all, more chaotic, first look at the structure:

-

This is more clear:

The entire XML is defined as a DataPacket, and the DataPacket includes two parts: Metadata and RowData. Obviously, RowData can ignore, and it is time to empty the node of RowData. Metadata?

See all XML formats, you can see: Metadata includes Fields and Params. That is, fields and data set parameters.

Field

First, you will focus on Fields. Look at the definition of the Field node, probably see some:

Attrname refers to FieldName FieldType means that the field type width is a parameter that requires a field type of width. If you need to define the properties of the field, add node

Let's take a look at the type of field, I think this is the most important! Other belongings are advanced.

MSSQL TypefieldtypeWIDHTSUBTYPEDECIMALSREADONLYBinarybin.hex50 Bitboolean Charstring10FixedChar DateTimedateTime Decimalfixed18 Floatr8 Imagebin.hex Binary Inti4 Moneyfixed19 4 nCharstring.uni20 nTextbin.hex Text Numericfixed18 nVarCharstring.uni100 SmallDateTimedateTime Realr8 SmallInti2 SmallMoneyfixed10 4 Textbin.hex Text TimeStampbin.hex8 truetinyInti2 UniqueIdentifierstring38Guid VarBinarybin.hex50Binary VarCharstring50 table above, which It is in SQLServer2000, uses all types of fields, and then the type corresponds to the CLIENTDATASET. Note that there is a Subtype property to help determine the type. There is no value for cells, indicating that the attribute is not used. In the width value, in addition to UNIQueIdentifier is 38, the rest use the default width of the system.

Analyze this sheet, you can get some features:

The suffix Uni represents Unicode, no suffix, indicating that the Ansicode suffix HEX represents the number of bits occupied by the number of integers, default is 4-bit R8 represents 8-bit floating point numbers

Field parameter

Note that there is a node under the definition of some fields:

Let's explain "provflags", look up in Delphi's source code, it is actually in response to TField's attribute provigroup, then 7 is well understood.

See the definition of TPROVIDERFLAG and TPROVIDERFLAGS

TPROVIDERFLAG = (pfinupdate, pfiNwhere, pfinkey, pfhidden);

TPROVIDERFLAGS = set of tproviderflag;

Since it is a collection, then 7 is obviously an integer representation: If the sequence is seminated, 7 is actually a combination of [pfinupdate, pfinwhere, pfinkey].

But I don't know why, check the properties of Tfield, PFINKEY is not in the collection.

Metadata properties

Look at

You can see the meaning of default_order and primary_key, but what is the value? After research, it was found that it was index of the primary key or index field. If both fields are indexes, its format is: "1 2", and the middle is separated by spaces.

How to use XML format?

Ok, the format is probably known, how is the problem now reproduced to the clientDataSet?

The file is ok, but it is obviously not to meet our original needs, check the interface definition, and find that there is LoadFromstream except for LoadFromFile. Check out the source code, in fact, LoadFromFile is called LoadFromstream. Ok, as long as we create such an XML stream, you can create a clientDataSet!

As for how to create an XML stream, don't discuss this

转载请注明原文地址:https://www.9cbs.com/read-28655.html

New Post(0)