views:

641

answers:

4

Is it possible to create and use a TClientDataSet inside an Object at runtime?

I like to make several changes in my Table and have those all Applied at the same time in cache like way, and TClientDataSet lets me do that. Know when I want to do this I have to build a TForm.

Is it Possible?

UPDATE

Can it be used, and How, without TDataSetProvider, and no TSQLQuery ? Because I tried it and it gave me an error no Provider!!

+1  A: 

Yes you can do, TClientDataSet is non visual component, and not designed to be used only inside forms.

You can build a unit (.pas without .dfm) than has classes and methods that can used TClientDataSet and return it also as parameters.

Mohammed Nasman
+2  A: 

Of course you can do that. But you can also considder using a data module. You can drag non visible components to a datamodule and use the object inspector to set the values.

Gamecat
+4  A: 

Components are just classes, and you can use them likewise:

procedure TMyObject.DoSomeDBStuff;
var
  localClientDataset: TClientDataset;
begin
  localClientDataset := TClientDataset.Create( );
  try

  finally
    localClientDataset.Free;
  end;
end;

You can also make a clientdataset-property if you like:

type
  TMyObject = class
  private
    FClientDB: TClientDataset;
  published
    property Dataset: TClientDataset read FClientDB;
  end;

Some visual components may require a visual parent though, but for TClientDataset there should be no such requirement.

Vegar
+2  A: 

You can create a TClientDataset at runtime. (See Vegar's answer.) As for the provider issue, the solution is to define fields for it, then open the dataset with the CreateDataset method (not the Open method!) and then it'll work.

Mason Wheeler