DataTable component (e.g. DataSet) in Delphi

I am a Delphi developer and C # developer. C # has a DataTable class that supports random access to strings. Is there a third-party component TDataSet (Delphi) that is similar to DataTable (C #)?

+3
source share
9 answers

There is a TClientDataSetclass in Delphi, the functionality of which is similar to that DataSetin .NET.

+7
source

You can use TADODataSetfor disconnected dataset in memory as suggested by Ian . This is very powerful, unlike TClientDataSetIMO.

But all low-level materials are ADOnot needed. it's simple:

var 
   ds: TADODataSet;

ds := TADODataSet.Create(nil);

//add our fields
ds.FieldDefs.Add('InvoiceNumber', ftInteger);
ds.FieldDefs.Add('CustomerName',  ftWideString, 200);
ds.FieldDefs.Add('CreatedDate',   ftDateTime);
ds.FieldDefs.Add('Comments',      ftWideMemo);
ds.FieldDefs.Add('Quantity',      ftFloat);
ds.FieldDefs.Add('InvoiceTotal',  ftCurrency);
ds.CreateDataSet;

//Add a row of values - the easy way
ds.Append;
ds.FieldByName('InvoiceNumber').AsInteger := 1783;
ds.FieldByName('CustomerName').AsString   := 'Hubert Farnsworth';
ds.FieldByName('CreatedDate').AsDateTime  := Now;
ds.FieldByName('Comments').AsString       := 'The quick brown fox jumped over the lazy dog';
ds.FieldByName('Quantity').AsFloat        := 19809.32; //imperial gallons
ds.FieldByName('InvoiceTotal').AsCurrency := 99.95; //GBP
ds.Post;

//Add another row of values - with an array of values all at once
ds.AppendRecord([1784, 'Steven Gates', Now, '//no comment', 1292, 19.25]);

You can also edit existing data:

ds.First;
ds.Edit;
ds.FieldByName('InvoiceNumber').AsInteger := 1786;

TDataSource, TADODataSet , , TDataSource.

+2

AnyDAC. , ADO.NET v 1, . TADMemTable.

PS: AnyDAC - .

+1

kbmMemTable CodeGear Edition, kbmMW CodeGear Edition, https://portal.components4developers.com ( cert. .. ).

kbmMemTable CodeGear Edition , Delphi.. . :

  • SQL ,
  • master/detail ( )
  • ,
  • deltahandlers

kbmMemTable Standard Edition, , Delphi, kbmMemTable Professional Edition kbmMW Professional Edition kbmMW Enterprise Edition.

www.components4developers.com

+1

JVCL TjvMemoryData, . , Delphi TClientDataSet.

+1

Cary Jensen, ClientDataSets DataTables: DataSet: ClientDataSet .NET DataTable : 1

+1

( ) AidAim

SQLMemTable - ; , , , ( ), . SQLMemTable BDE .

( ) TxQuery (MPL)

TxQuery TDataSet, TDataSet SQL

+1

, TDataSet. TClientDataSet ( ) , DLL, . ( , dll dongle).

, Microsoft DataTable : ADO Recordset.

ADO Recordset, , TADODataSet, Delphi TDataSet ( ADO Recordset ).

var
    rs: Recordset;

//Use ADO Recordset to hold our in-memory table
rs := CoRecordset.Create;
rs.CursorLocation := adUseClient; //default is adUseServer

, , :

//add our fields
rs.Fields.Append('InvoiceNumber', adInteger,      0,   adFldUpdatable, EmptyParam);
rs.Fields.Append('CustomerName',  adVarWChar,     200, adFldUpdatable, EmptyParam);
rs.Fields.Append('CreatedDate',   adDBTimeStamp,  0,   adFldUpdatable, EmptyParam);
rs.Fields.Append('Comments',      adLongVarWChar, -1,  adFldUpdatable, EmptyParam);
rs.Fields.Append('Quantity',      adDouble,       0,   adFldUpdatable, EmptyParam);
rs.Fields.Append('InvoiceTotal',  adCurrency,     0,   adFldUpdatable, EmptyParam);

Open :

var
   o: OleVariant;

//It impossible in Delphi to omit parameters. So we do it the late-binding IDispatch way
//  rs.Open(EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam);
o := (rs as IDispatch);
o.Open;

. :

//Add a row of values - the easy way
rs.AddNew(EmptyParam, EmptyParam); //moves cursor to newly added row
rs.Fields['InvoiceNumber'].Value := 1783;
rs.Fields['CustomerName'].Value  := 'Hubert Farnsworth';
rs.Fields['CreatedDate'].Value   := Now;
rs.Fields['Comments'].Value      := 'The quick brown fox jumped over the lazy dog';
rs.Fields['Quantity'].Value      := 19809.32; //imperial gallons
rs.Fields['InvoiceTotal'].Value  := 99.95; //GBP

//Add another row of values - you can add by single value
rs.AddNew('InvoiceNumber', 1784);

//Add another row of values - you can add by field names
rs.AddNew(VarArrayOf(['InvoiceNumber', 'InvoiceTotal']), VarArrayOf([1784, 22.37]));

//Add another row of values - you can add by ordinal index
rs.AddNew(VarArrayOf([0, 2]), VarArrayOf([1785, Now]));

//Move to the start of the Recordset, so it will be ready for the person using it.
if (not rs.BOF) or (not rs.EOF) then //You can't MoveFirst on a Recordset if the Recordset is empty. (It throws an error instead of not throwing an error)
rs.MoveFirst;

, , TDataSet:

var
   dataset: TDataSet;      

//Wrap the recordset is a TDataSet descendant 
//that knows how to talk to an ADO recordset: the TADODataSet.
dataset := TADODataSet.Create(nil);
dataset.Recordset := rs;

, :

var 
   ds: TDataSet;
begin
   ds := CreateMemoryDataSet();
   ShowMessage(DataSetToMarkdown(ds));

:

| InvoiceNumber | CustomerName      | CreatedDate          | Comments | Quantity | InvoiceTotal |
|---------------|-------------------|----------------------|----------|----------|--------------|
| 1783          | Hubert Farnsworth | 7/25/2017 3:32:21 PM | The quick brown fox jumped over the lazy dog | 19809.32 | 99.95 |
| 1784          |                   |                      |          |          |              |
| 1784          |                   |                      |          |          | 22.37        |
| 1785          |                   | 7/25/2017 3:32:22 PM |          |          |              |

:

ds := CreateMemoryDataSet();    
ds.First;
ds.Edit;
ds.FieldByName('InvoiceNumber').AsInteger := 1786;
ShowMessage(DataSetToMarkdown(ds));

| InvoiceNumber | CustomerName      | CreatedDate          | Comments | Quantity | InvoiceTotal |
|---------------|-------------------|----------------------|----------|----------|--------------|
| 1786          | Hubert Farnsworth | 7/25/2017 3:32:21 PM | The quick brown fox jumped over the lazy dog | 19809.32 | 99.95 |
| 1784          |                   |                      |          |          |              |
| 1784          |                   |                      |          |          | 22.37        |
| 1785          |                   | 7/25/2017 3:32:22 PM |          |          |              |
+1

You might be interested in the TECDataset (EverClassy dataset) from Inovativa ( http://www.inovativa.com.br/public ), which is a dataset in memory that can be filled with objects of any class.

0
source

All Articles