Search code examples
sql-serverdelphiblobdocuments

restore documents which are stored as blobs in a sql database


I've got an sql database from a customer. In the sql database is a table, called "documents". The documents are stored as blobs (have a look at the screenshot).

I want to restore the documents via Delphi (FileStream?). Do you have any ideas how to do that?

Regards, Dennis Friedrich

Screenshot table documents


Solution

  • function SaveFieldBlobToFile(FQuery: TDataSet): boolean;
    var MyStream : TMemoryStream;
    begin
       Result:= false;
       MyStream:= TMemoryStream.Create;
       try
         (FQuery.Fieldbyname('MODELE') as TBlobField).SaveToStream(MyStream);
        if (MyStream<>nil) and (MyStream.Size > 0) then
        begin
          MyStream.Position := 0;
          MyStream.SaveToFile('C:\Temp\tempMyFile.zip');
          Result:= true;
        end
        else Result:= false;
      finally
        MyStream.Free;
      end;
    end;
    
    
    function LoadFileToBlob(FQuery: TDataSet): boolean;
    var MyStream : TMemoryStream;
    begin
      Result:= false;
      MyStream := TMemoryStream.Create;
      try
        MyStream.LoadFromFile('C:\Temp\tempMyFile.zip');
        MyStream.Position := 0;
        TryEdit(FQuery);
        (FQuery.FieldByName('MODELE') As TBlobField).LoadFromStream(MyStream);
        TryPost(FQuery);
        Result := True;
      finally
        MyStream.Free;
      end;
    end;