Thema: Delphi Blob in DB Speichern

Einzelnen Beitrag anzeigen

Benutzerbild von peschai
peschai

Registriert seit: 15. Feb 2004
Ort: Göppingen
270 Beiträge
 
Delphi XE5 Professional
 
#3

Re: Blob in DB Speichern

  Alt 14. Feb 2008, 06:41
Hallo
Schematisch in etwa so

"DataSet" symbolisiert deine Tabelle ...

1.) Daten in Stream zwischenlagern
2.) Zum schreiben öffnen mit "Edit"
3.) Stream in Blobfield schreiben
4.) Schreiben abschliessen mit "Post"

Delphi-Quellcode:
function MyObject.FieldWriteAsBlob(const FieldName:String;Const aStream:TStream):Boolean;
var
  j1:TField;
begin
  Result := False;
  j1 := Nil;
  Try
    if Assigned(DataSet) then
      begin
        j1 := DataSet.FieldByName(FieldName);
        If assigned(j1) then
          begin
            if j1.IsBlob then
               begin
                 DataSet.Edit;
                 if Assigned(aStream)
                   then TBlobField(j1).LoadFromStream(aStream)
                   else TBlobField(j1).Clear;
                 DataSet.Post;
                 Result := True;
               end;
          end;
        end;
    except
      on e:exception do
        begin
          Result := False;
          DataSet.Cancel;
          //.... Exception melden ?
        end;
    end;
end;

procedure MyObject.btnPictureWriteClick(Sender: TObject);
var
  ms1:TMemoryStream;
  s2:STring;
  i9:integer;
begin
  // local init
  ms1 := Nil;
  s2 := '';
  i9 := 0;
  { Action }
  try
    try
      if Assigned(DataSet) then
        begin
          if Not(DataSet.ReadOnly) then
            begin
              DataSet.Activate;
              ms1 := TMemoryStream.Create;
              if OpenPictureDialog1.Execute then
                begin
                  ms1.LoadFromFile(OpenPictureDialog1.FileName);
                  ms1.Seek(0, soFromBeginning);
                  MyObject.FieldWriteAsBlob('BlobFeldName',ms1);
                  ms1.Clear;
                end;
            end;
        end;
    except
      on E:Exception do
        begin
          s2 := e.Message;
          // ....
        end;
    end;
  finally
    FreeAndNil(ms1);
  end;
end;
Peter Schaible
  Mit Zitat antworten Zitat