Discussion:
DBExpress to ClientDataset
(too old to reply)
jrodenhi
2007-11-30 07:20:17 UTC
Permalink
I am working on a reporting application. I would like to enable my
users to retrieve data with one or more SQL queries and store that
data in one or more ClientDatasets. So the fields will be determined
at runtime. Then I want to either use that data in FastReports or use
it in other forms of output like PDF forms. I'm getting tripped up by
what is probably a dumb problem. Here's my code:

procedure TfrmQuery.LoadDataset(iNdx: Integer; sSQL: String);{$O-}
var iField: Integer;
sArray: TStrArray;
begin
Query.SQL.Clear;
Query.SQL.Add(sSQL);
Query.Open;
if iNdx > High(Datasets) then SetLength(Datasets, Succ(iNdx));
if Datasets[iNdx] = nil then Datasets[iNdx] :=
TClientDataSet.Create(nil);
Datasets[iNdx].Name := 'Dataset' + IntToStr(iNdx);
CreateDatasetFields(Query, Datasets[iNdx]);
while not Query.Eof do begin
Datasets[iNdx].Append;
for iField := 0 to Datasets[iNdx].FieldCount - 1 do begin
Datasets[iNdx].Fields[iField].Assign(Query.Fields[iField]);
end;
Datasets[iNdx].Post;
Query.Next;
end;
sArray := CArray(Datasets[iNdx]);
end;

CArray is utility routine that copies a ClientDataset into a string
array for debugging. My problem is that, at the end of this routine,
sArray looks like
('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '') and if I try to write the
ClientDataset to a string grid, I get nothing.

So what I want to do is to fill a ClientDataset from an arbitrary
query and disconnect from the database to work with the data. Any
thoughts on what is wrong with this code?

Thanks for your help.

-Jack
jrodenhi
2007-11-30 17:35:31 UTC
Permalink
I found the answer on page 2013 of the Users Guide PDF that came with
Delphi 2006. You just have to pass the data through a DataProvider.
For anyone who is interested here is the example code:

TempProvider := TDataSetProvider.Create(Form1);
TempProvider.DataSet := SourceDataSet;
ClientDataSet1.Data := TempProvider.Data;
TempProvider.Free;

So, here is how I modified my procedure:

procedure TfrmQuery.LoadDataset(iNdx: Integer; sSQL: String);{$O-}
var iField: Integer;
sArray: TStrArray;
TempProvider: TDataSetProvider;
begin
if iNdx > High(Datasets) then SetLength(Datasets, Succ(iNdx));
if Datasets[iNdx] = nil then Datasets[iNdx] :=
TClientDataSet.Create(nil);
Datasets[iNdx].Name := 'Dataset' + IntToStr(iNdx);
Query.SQL.Clear;
Query.SQL.Add(sSQL);
Query.Open;
TempProvider := TDataSetProvider.Create(nil);
TempProvider.DataSet := Query;
Datasets[iNdx].Data := TempProvider.Data;
TempProvider.Free;
end;

-Jack
Post by jrodenhi
I am working on a reporting application. I would like to enable my
users to retrieve data with one or more SQL queries and store that
data in one or more ClientDatasets. So the fields will be determined
at runtime. Then I want to either use that data in FastReports or use
it in other forms of output like PDF forms. I'm getting tripped up by
procedure TfrmQuery.LoadDataset(iNdx: Integer; sSQL: String);{$O-}
var iField: Integer;
sArray: TStrArray;
begin
Query.SQL.Clear;
Query.SQL.Add(sSQL);
Query.Open;
if iNdx > High(Datasets) then SetLength(Datasets, Succ(iNdx));
if Datasets[iNdx] = nil then Datasets[iNdx] :=
TClientDataSet.Create(nil);
Datasets[iNdx].Name := 'Dataset' + IntToStr(iNdx);
CreateDatasetFields(Query, Datasets[iNdx]);
while not Query.Eof do begin
Datasets[iNdx].Append;
for iField := 0 to Datasets[iNdx].FieldCount - 1 do begin
Datasets[iNdx].Fields[iField].Assign(Query.Fields[iField]);
end;
Datasets[iNdx].Post;
Query.Next;
end;
sArray := CArray(Datasets[iNdx]);
end;
CArray is utility routine that copies a ClientDataset into a string
array for debugging. My problem is that, at the end of this routine,
sArray looks like
('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
'', '', '', '', '', '', '', '', '') and if I try to write the
ClientDataset to a string grid, I get nothing.
So what I want to do is to fill a ClientDataset from an arbitrary
query and disconnect from the database to work with the data. Any
thoughts on what is wrong with this code?
Thanks for your help.
-Jack
Loading...