Friday, October 30, 2009

How to pass a stream to a database query

In previous post I've shown you how to save a TRichEdit/TDBRichEdit or descendant to a TStream, now heres how to pass that stream to a INSERT query
// your procedure/function
var stream : TStream;
begin
     // the insert query
     myQuery.SQL.Text := 'INSERT INTO "Mytable"("MyStreamField") VALUES(:RichEdit)';
     // get TRichEdit as stream with helper function
     stream := RichEditToStream(myRichEdit);
     // create a new parameter with field type as ftBlob and parameter
     // type as ptInput
     myQuery.Params.CreateParam(ftBlob, 'RichEdit', ptInput);
     // now load the parameter from stream
     // we can also use myQuery.myQuery.Params[0].LoadFromStream();
     myQuery.ParamByName('RichEdit').LoadFromStream(stream, ftBlob);
     // we free the stream here
     FreeAndNil(stream);
     // execute the sql
     myQuery.ExecSQL;
     // clear the parameters
     myQuery.Params.Clear;
end;

No comments:

Post a Comment

Blogroll(General programming and Delphi feeds)