Hi Everyone
I had quite a struggle today to figure this one out but in the end it
worked. I needed to save parameters of a query for later use but the
TStream.WriteComponentRes only supported writing the data from objects
inheriting from TComponent.
Here is my solution :
I created a class called TParamSave. Notice the published property
Params which is needed for WriteComponentRes to "save" it.
TParamSave = class(TComponent)
private
FParams : TParams;
public
constructor Create(AOwner: TComponent); override;
published
property Params : TParams read FParams write FParams;
end;
Under Implementation is the very basic constructor for the Params,
otherwise we have problems later
{ TParamSave }
constructor TParamSave.Create(AOwner: TComponent);
begin
inherited;
Params := TParams.Create;
end;
Well all you need to do now is save it to a stream / file or database
field. Here is my code below, I am saving it to a database blob field
but I tested it to files also.
var
Stream : TStream;
ParamSave : TParamSave;
begin
try
Stream := TMemoryStream.Create;
ParamSave := TParamSave.Create(Self);
ParamSave.Name := 'Param';
ParamSave.Params := ExtraParams; //ExtraParams is my parameters
from a query
Stream.WriteComponentRes('Params', ParamSave);
......insert sql here ....
qryQuery.ParamByName ('extraparams').LoadFromStream(Stream, ftBlob);
qryQuery.ExecSQL;
finally
Stream.Free;
ParamSave.Free;
end;
end;
And then finally to read it back into the application
var
ExtraParams : TParams;
ParamSave : TParamSave;
Stream : TStream;
begin
try
Stream := TMemoryStream.Create;
(qryBatchPrint.FieldByName('extraparams') as
TBlobField).SaveToStream (Stream);
ExtraParams := TParams.Create;
ParamSave := TParamSave.Create(Self);
Stream.Position := 0;
Stream.ReadComponentRes (ParamSave);
ExtraParams.Assign(ParamSave.Params);
finally
Stream.Free;
ExtraParams.Free;
end;
end;
And thats it !
I hope this helps someone else who got stuck on this issue.
__________________________________________________
Delphi-Talk mailing list -> Delphi-Talk@elists.org
http://www.elists.org/mailman/listinfo/delphi-talk