Creating an in-memory dataset involves defining its metadata and constructing its data store. Regardless of which in-memory dataset you are creating, this can be achieved in one of two ways. You can both define the metadata and construct the data store programmatically, or you can load the dataset from a query result set. When you load the dataset from a query result set, the contents of the query define the dataset's metadata and the data store is created by the method used to load the data.
With ClientDatasets you have an additional capability, that of defining the metadata at design time. This can be done either by using the Fields Editor to define TField definitions or by using the collection editor for the FieldDefs property to define TFieldDefs.
No matter which design time approach you take (and these are not exclusive options, you can use a combination of TFields and TFieldDefs to define a ClientDataSet’s metadata), you can then right-click the ClientDataSet in the Delphi designer and select Create DataSet to create a design time instance of its data store.
Though the design time definition of a DataTable’s structure is not an option, DataTables support another feature that ClientDataSets do no. Specifically, after you load a DataTable from a query result set at runtime, you can then add additional metadata definitions to configure additional DataColumns. For ClientDataSets, once they have been instantiated and their data store created, no further change can be made to its columns (represented by TField instances).
But before going further, it is worth nothing that this discussion of DataTables (as well as future discussions of DataViews, DataSets, and related ADO.NET classes) apply both to Delphi Prism, Delphi for .NET, as well as other first class .NET classes (such as C# and VB for .NET). However, the syntax of the code samples is slightly different in some cases. Since Delphi Prism is the current (and preferred) Delphi solution for building .NET applications, the examples in this series will use Delphi Prism syntax, that of the Oxygene compiler.
The following code segment demonstrates how to create a ClientDataSet programmatically using its FieldDefs.AddFieldDef method.
ClientDataSet1 := TClientDataSet.Create(Self);
with ClientDataSet1.FieldDefs do
begin
Clear;
with AddFieldDef do
begin
Name := 'ID';
DataType := ftInteger;
end; //with AddFieldDef do
with AddFieldDef do
begin
Name := 'Name';
DataType := ftString;
Size := 30;
end; //with AddFieldDef do
with AddFieldDef do
begin
Name := 'Date of Birth';
DataType := ftDate;
end; //with AddFieldDef do
with AddFieldDef do
begin
Name := 'Active';
DataType := ftBoolean;
end; //with AddFieldDef do
end; //with ClientDataSet1.FieldDefs
ClientDataSet1.CreateDataSet;
Here is another example that uses the FieldDefs.Add method. This example creates a ClientDataSet identical to the one created in the preceding code.
ClientDataSet1 := TClientDataSet.Create(Self);
with ClientDataSet1.FieldDefs do
begin
Clear;
Add('ID',ftInteger, 0, True);
Add('First Name',ftString, 30);
Add('Date of Birth',ftDate);
Add('Active',ftBoolean);
end; //with ClientDataSet1.FieldDefs
ClientDataSet1.CreateDataSet;
Here is an example of code that creates a .NET DataTable.
var DataTable1: DataTable := DataTable.Create;
var DataColumn1: DataColumn := DataColumn.Create('CustNo',
System.Type.GetType('System.Int32'));
DataColumn1.AllowDBNull := False;
DataColumn1.Unique := True;
DataTable1.Columns.Add(DataColumn1);
DataTable1.Columns.Add('FirstName',
System.Type.GetType('System.String'));
//Here is another way to define the type
DataTable1.Columns.Add('LastName', TypeOf(String));
DataTable1.Columns['LastName'].DefaultValue := 'Not assigned';
The next article in this series will demonstrate how to populate an in-memory dataset once it has been created.
Copyright © 2009 Cary Jensen. All Rights Reserved
I used to play around a lot with ClientDataSet, you and the articles you've written in the past were my guide. You made it very easy to learn. Looking forward to more.
ReplyDeleteThanks for the article.