Recent

Author Topic: Writing a StringGrid into a DB table cell  (Read 3570 times)

Bert_Plitt

  • Jr. Member
  • **
  • Posts: 62
Writing a StringGrid into a DB table cell
« on: February 03, 2015, 07:38:05 pm »
Help needed!  I'm new to Laz/FPC.  Is it possible to write into (and read from) a BLOB cell in a database table a complete StringGrid?  If so, can you show me some example code that does it?
Windows 10, Lazarus 2.2.2, FPC 3.2.2

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Writing a StringGrid into a DB table cell
« Reply #1 on: February 03, 2015, 09:18:38 pm »
It is... (not sure if it's the right way to do for your purpose)
You'll need to use a stream as an intermediate.

Something like this (with two functions to save and read a complete stringgrid to/from a BLOB-field):
Code: [Select]
procedure SaveStringGridToField(AGrid: TStringGrid; AField: TField);
var
  AStream: TMemoryStream;
begin
  AStream := TMemoryStream.Create;
  try
    AGrid.SaveToStream(AStream);
    AStream.Position := 0;
    TBlobField(AField).LoadFromStream(AStream);
  finally
    AStream.Free;
  end;
end;

procedure LoadStringGridFromField(AGrid: TStringGrid; AField: TField);
var
  AStream: TMemoryStream;
begin
  AStream := TMemoryStream.Create;
  try
    TBlobField(AField).SaveToStream(AStream);
    AStream.Position := 0;
    AGrid.LoadFromStream(AStream);
  finally
    AStream.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SQLQuery1.Edit;
  SaveStringGridToField(StringGrid1, SQLQuery1.FieldByName('BLOB'));
  SQLQuery1.Post;
  // and apply etc...
end;

Bert_Plitt

  • Jr. Member
  • **
  • Posts: 62
Re: Writing a StringGrid into a DB table cell
« Reply #2 on: February 04, 2015, 04:19:10 am »
RVK --
Thanks so much.  Works like a charm!
Windows 10, Lazarus 2.2.2, FPC 3.2.2

 

TinyPortal © 2005-2018