title: Save/Load objects as blob in a database
body: <p>Whats wrong with my code? I try to save and then load again an object to a blob field in a database, but get nothing back. </p>
  
  <p>Records are saved, but I can't say if the data was written correctly because I cant read the data back.</p>
  
  <p>Here is the object type:</p>
  
  <pre><code>TMyObject = class
    Name: string;
  end;
  </code></pre>
  
  <p>And here I try to save:</p>
  
  <pre><code>procedure TForm1.btnSaveObjectClick(Sender: TObject);
  var
    myObject: TmyObject;
    aMemoryStream: TMemoryStream;
  begin
    myObject:= TMyObject.Create;
    myObject.Name:=edtName.Text;
    aMemoryStream:= TMemoryStream.Create;
    aMemoryStream.Write(myObject, myObject.InstanceSize);
    aMemoryStream.Position:=0;
  
    with TSQLQuery.Create(nil) do
    begin
      DataBase:=Conn;
      SQL.Text:='INSERT INTO testtable (data) VALUES (:data)';
      try
        ParamByName('data').LoadFromStream(aMemoryStream, ftBlob);
        ExecSQL;
        TX.CommitRetaining;
      finally
        aMemoryStream.Free;
        myObject.Free;
        Free;
      end;
    end;
  end;
  </code></pre>
  
  <p>Trying to read the data back up again:</p>
  
  <pre><code>procedure TForm1.btnLoadObjectClick(Sender: TObject);
  var
    myObject: TMyObject;
    BlobStream : TStream;
  begin
    with TSQLQuery.Create(nil) do
    begin
      DataBase:=Conn;
      SQL.Text:='SELECT data FROM testtable';
      myObject:= TmyObject.Create;
      try
        Open;
        Last;
        BlobStream:= CreateBlobStream(FieldByName('data'), bmread);
        BlobStream.Position:=0;
        BlobStream.Read(myObject, BlobStream.Size);
        ShowMessage('Stored Name: ' +myObject.Name);
      finally
        myObject.Free;
        Free;
      end;
    end;
  end;
  </code></pre>
  
  <p>Also, should BlobStream be free'd?</p>