将图像保存到记录中

时间:2013-02-03 22:55:43

标签: delphi ms-access delphi-xe3

我正在将一些信息存储到Access数据库中,一个参数是一个BLOB字段,在这种情况下是一个图像,加载到Timage中。

我正在使用此代码保存它:

var
AStream : TMemoryStream;
begin
AStream := TMemoryStream.Create;
  try
Image1.Picture.Graphic.SaveToStream(AStream);
AStream.Position := 0;
if Adotable1.Active then
begin
  Adotable1.Edit;
  TBlobField(Adotable1.FieldByName('Termograma')).LoadFromStream(AStream);
  Adotable1.Post;
end;
  finally
    AStream.Free;

adotable1.Append;
adotable1['Data']:= datetimepicker1.Date;
adotable1['Temax']:= edit4.Text;
adotable1['Temin']:= edit5.Text;
adotable1['Descrição da Posição']:= memo1.Text;
adotable1['Comentários']:= memo2.Text;
adotable1.Post;

但是我也有其他信息我通过点击相同的按钮来存储,如“追加”部分。

当我按下保存按钮时,该信息不会存储在数据库中的相同ID中。

如何更正此问题?

1 个答案:

答案 0 :(得分:5)

您正在编辑当前记录,将图像保存到该记录,附加新记录,并将其余信息保存到该新记录中。我想你打算添加一个完整的新记录,将图像和数据添加到新记录中,然后保存这些更改。

请改为尝试:

var
  AStream : TMemoryStream;
begin
  if not AdoTable1.Active then
    AdoTable1.Open;

  Adotable1.Append;

  AStream := TMemoryStream.Create;
  try
    Image1.Picture.Graphic.SaveToStream(AStream);
    AStream.Position := 0;
    TBlobField(Adotable1.FieldByName('Termograma')).LoadFromStream(AStream);
  finally
    AStream.Free;
  end;

  adotable1['Data']:= datetimepicker1.Date;
  adotable1['Temax']:= edit4.Text;
  adotable1['Temin']:= edit5.Text;
  adotable1['Descrição da Posição']:= memo1.Text;
  adotable1['Comentários']:= memo2.Text;
  adotable1.Post;
end;