Delphi dynamically creates clientdataset table Definitions

Source: Internet
Author: User
Preface

Many people are asking how clientdataset can be created with a program without connecting to the database and open the dataset.

After studying the tclientdataset dataset, we found that if you want to enable clientdataset (open), one of the three conditions must be met:

  1. Providername attribute value assignment, that is, data source provider.
  2. Data Attribute assignment. That is, the table structure and data are obtained from other opened data sets.
  3. Filename Value assignment: obtains data and metadata from a local file.

The three conditions are described in the help of the active attribute of tclientdataset. Think about it. First and second are basically excluded. What should I do if I have a ready-made dataset? Third, it's not that easy. How do you know the file format?

So what should we do?

Failed attempts

It suddenly reminds me of the support for internalcalc (internal calculation) fields in tclientdataset. Internal calculation fields can be added dynamically during the design period or during the runtime. We joined in the design period for convenience. See:

You can double-click the control (tclientdataset) to enter the field editor, right-click and choose "New field. Enter name, type, and fieldtype.

Remember: fieldtype must be "internalcalc ". You can choose to add multiple fields.

Open! Error: "providername" or data is not assigned a value.

This method is incorrect. Since data is required, I try to assign values from the data attribute of other clientdataset. The result is that the newly created fields and the fields of the data source are displayed.

I made another attempt: assign a value to the data of the clientdataset with the newly created field and the original field to another new clientdataset, but I found that the newly created field is not entered.

In general, this attempt fails, but it is summarized as follows:

  1. Creating an internal calculated field is not feasible.
  2. The new calculated field cannot enter the data attribute and is passed to the second clientdataset.
XML

XML again! Clientdataset is a disconnected dataset control. The savetofile () function of caching data to a disk is provided. Its detailed declaration is as follows:

  procedure SaveToFile(const FileName: string = ''; Format: TDataPacketFormat = dfBinary);

Note the tdatapacketformat. It is an enumeration type. All enumeration values are as follows:

  TDataPacketFormat = (dfBinary, dfXML, dfXMLUTF8);

You may have noticed that both dfbinary and dfxmlutf8 are not our concern. Let's take a look at our dfxml format. This is an exciting news. With XML, we can view the definition of metadata in clientdataset, and it is more likely to be modified!

Call the savetofile function. Note that the Save format is dfxml. Open the file, as shown below:

  <?xml version="1.0" standalone="yes" ?> - <DATAPACKET Version="2.0">- <METADATA>- <FIELDS>-  <FIELD attrname="NormInfoID" fieldtype="i4">    <PARAM Name="PROVFLAGS" Value="7" Type="i4" Roundtrip="True" />    </FIELD>   <FIELD attrname="Description" fieldtype="string.uni" WIDTH="160" />    <FIELD attrname="NormInfoVal" fieldtype="string.uni" WIDTH="510" />    <FIELD attrname="NewField" fieldtype="i4" />    <FIELD attrname="Boolean" fieldtype="Boolean" />    <FIELD attrname="Date" fieldtype="date" />    <FIELD attrname="Time" fieldtype="time" />   </FIELDS>  <PARAMS DEFAULT_ORDER="1" PRIMARY_KEY="1" />   </METADATA>  <ROWDATA />   </DATAPACKET>

Looking at all this is confusing. Let's look at the structure first:

  <?xml version="1.0" standalone="yes" ?> - <DATAPACKET Version="2.0">  + <METADATA>    <ROWDATA />   </DATAPACKET>

This is clear:

The entire XML definition is a datapacket. datapacket consists of two parts: Metadata and rowdata. Obviously, we can ignore rowdata, so that we can clear rowdata nodes. What about metadata?

Looking at all the XML formats, we can see that metadata includes fields and Params. Fields and dataset parameters.

Field

First, we should focus on fields. Let's take a look at the definition of the field node. We can see that:

  1. Attrname refers to fieldname
  2. Fieldtype indicates the field type.
  3. Width is a parameter of the field type that requires width.
  4. If you need to define the attribute of a field, add the <Param/> node

Next, let's take a look at the field type. I think this is also the most important! Others are advanced usage.

MSSQL type Fieldtype Widht Subtype Decimals Readonly
Binary Bin. HEX 50      
Bit Boolean        
Char String 10 Fixedchar    
Datetime Datetime        
Decimal Fixed 18      
Float R8        
Image Bin. HEX   Binary    
Int I4        
Money Fixed 19   4  
Nchar String. Uni 20      
Ntext Bin. HEX   Text    
Numeric Fixed 18      
Nvarchar String. Uni 100      
Smalldatetime Datetime        
Real R8        
Smallint I2        
Smallmoney Fixed 10   4  
Text Bin. HEX   Text    
Timestamp Bin. HEX 8     True
Tinyint I2        
Uniqueidentifier String 38 Guid    
Varbinary Bin. HEX 50 Binary    
Varchar String 50      

As shown in the preceding table, all types of fields are used in sqlserver2000 and the type ing relationship is obtained in clientdataset. Note that the subtype attribute is available to help determine the type. Cells with no values indicate that this attribute is not used. In the width value, except that the uniqueidentifier is 38, the system default width is used for all other values.

By analyzing this table, you can obtain the following features:

  1. The suffix uni indicates Unicode. If there is no suffix, it indicates ansicode.
  2. The suffix hex indicates hexadecimal
  3. The number after I indicates the number of digits occupied by the integer. The default value is 4 digits.
  4. R8 indicates an 8-bit floating point number.
FIELD PARAMETERS

Note that some fields have nodes defined:

<PARAM Name="PROVFLAGS" Value="7" Type="i4" Roundtrip="True" /> 

Let's first explain "provflags". In the source code of Delphi, we can find that it actually corresponds to the tfield attribute providerflags, so 7 is easy to understand.

See the definitions of tproviderflag and tproviderflags.

  TProviderFlag = (pfInUpdate, pfInWhere, pfInKey, pfHidden);  TProviderFlags = set of TProviderFlag;

Since it is a set, 7 is obviously an integer representation of a set: If it is arranged in order, 7 is actually a combination of [pfinupdate, pfinwhere, pfinkey].

But I don't know why. Check the tfield attributes. pfinkey is not in the collection.

Metadata attributes

Look

  <PARAMS DEFAULT_ORDER="1" PRIMARY_KEY="1" /> 

We can see the meaning of default_order and primary_key, but what about the following values? After research, we found that it is the index of the primary key or index field. If both fields are indexed, the format is "1 2", separated by spaces.

How to use XML format?

Well, the format is probably all known. Now the question is how to repost it to clientdataset?

The file is okay, but it obviously does not meet our original needs. Check the interface definition and find that besides loadfromfile, there is also loadfromstream. Check the source code. In fact, loadfromfile calls loadfromstream.

All right, as long as we have created such an XML Stream, We can dynamically create clientdataset!

How to create an XML Stream is not discussed here

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.