Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTable (like DataSet) component in Delphi

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

like image 426
MajidTaheri Avatar asked Dec 06 '22 15:12

MajidTaheri


2 Answers

There's TClientDataSet class in Delphi, which functionality is similar to DataSet in .NET.

like image 145
Dennis Avatar answered Dec 09 '22 05:12

Dennis


You can use TADODataSet for a disconnected in-memory dataset as suggested by Ian. It is very powerful as opposed to TClientDataSet IMO.

But all the low level ADO stuff is not needed. it is simple as:

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;

You can then use TDataSource to bind it to the TADODataSet and use data aware controls if needed linked to the TDataSource.

like image 34
kobik Avatar answered Dec 09 '22 05:12

kobik