Delphi將Image存入MySQL資料庫

存入

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

取出

var
Ms:TStringStream;
jpg1:Tjpegimage;
begin
   Ms := TStringStream.Create('');
   jpg1 := Tjpegimage.create;
  try
    if adoquery1.Active then
    begin
      TBlobField(adoquery1.FieldByName('File')).SaveToStream(Ms);
      Ms.Position := 0;
      jpg1.LoadFromStream(ms);
      image1.Picture.Bitmap.Assign(jpg1);      
    end;
  finally
    ms.Free;
    jpg1.Free;
  end;

注意,如果出現Got a packet bigger than ‘max_allowed_packet’ bytes
在My.ini加上
max_allowed_packet = 10M

我要留言

留言

發表迴響

你的電子郵件位址並不會被公開。