a.vakhrushev
a.vakhrushev

Reputation: 37

How to modify calculated field with TADODataSet?

I have a TADODataset executed with (only for example):

SELECT id, name, lastname, name + ' ' + lastname as fullname
FROM persons
ORDER BY lastname

After I open the dataset, I can modify "name" and "lastname" fields, but can't modify"fullname", because it's calculated.

I try to open TADODataset to TClientDataset via DataProvider, but it takes too long (there are about 100K records in source dataset):

SrcDS.FieldDefs.Update;
for i := 0 to Pred(SrcDS.FieldDefs.Count) do
  SrcDS.FieldDefs[i].CreateField(SrcDS).ReadOnly := false;
DestDS := TClientDataset.Create(nil);
DestDS.SetProvider(SrcDS);
DestDS.Open;
DestDS.SetProvider(nil);

All in all, i want to have an independent dataset with changeable fields.
How can i modify calculated fields in the dataset?

Upvotes: 2

Views: 1920

Answers (1)

alzaimar
alzaimar

Reputation: 4622

You have to calculated the field in Delphi. Create a new field by rightclicking on the TADODataset component, select New Field, give it a name and set it's type to 'calculated'.

In the OnCalculateFields-Event simply write:

Procedure TMyDataModule.MyDatasetCalculate(Sender : TDataset);
Begin
  MyDataSetFullName.AsString := MyDatasetFirstName.AsString+' '+MyDataSetLastName.AsString;
End;

Update: Regarding your second problem (100.000 records): If you load them into your ADODataset using LockType = ltBatchOptimistic, it will be fast enough and nothing is saved to the database, unless you call the UpdateBatch Method.

If this is still too slow, try using the async load feature (See the ExecuteOptions)

Upvotes: 2

Related Questions