Einzelnen Beitrag anzeigen

hoika

Registriert seit: 5. Jul 2006
Ort: Magdeburg
8.276 Beiträge
 
Delphi 10.4 Sydney
 
#6

Re: Probleme mit TQuery und Records als Blob

  Alt 7. Apr 2008, 19:16
Hallo,

hier 2 meiner Routinen


Heiko

Delphi-Quellcode:

{
name:
  UpdateTextBlobField_Ex
usage:
  update a blob field
parameter:
  theTableName      - table name
  thePrimaryKeyName  - name of the primary key
  thePrimaryKeyValue - value of the primary key
  theFieldName      - field name of the blob field
  theText            - field value
return parameter:
  theErrorStr - error message
return:
  false on error
notes:
  - additional to UpdateTextBlobField name of the
    primary key is needed
}

function UpdateTextBlobField_Ex(const theTableName: String;
  const thePrimaryKeyName: String; thePrimaryKeyValue: Integer;
  const theFieldName: String; const theText: String;
  var theErrorStr: String): Boolean;
var
  FQuerySQL : TQuery;
begin
  Result:= False;
  theErrorStr:= S_internal_error;

  try
    FQuerySQL := CreateQuery; // TQuery.Create ...
    try
      with FQuerySQL do
      begin
        DataBaseName:= C_ALIASNAME; // Konstante

        SQL.Add('Update '+theTableName+' Set ');
        SQL.Add(theFieldName+'=:'+theFieldName);
        SQL.Add('Where '+thePrimaryKeyName+'=:Id');
        ParamByName('Id').AsInteger:= thePrimaryKeyValue;
        if theText='then
        begin
          ParamByName(theFieldName).DataType:= ftBlob;
          ParamByName(theFieldName).Clear;
          ParamByName(theFieldName).Bound:= True;
        end
        else
        begin
          ParamByName(theFieldName).AsBlob:= theText;
        end;
        ExecSQL;
      end; { with FQuerySQL do }

      Result:= True;
    finally
      FQuerySQL.Free;
    end;
  except
    on E: Exception do theErrorStr:= E.message;
  end;
end; { UpdateTextBlobField_Ex }

{
name:
  LoadTextBlobField_Ex
usage:
  load the text from the blob field
parameter:
  theTableName      - table name
  thePrimaryKeyName  - name of the primary key
  thePrimaryKeyValue - value of the primary key
  theFieldName      - field name of the blob field
return parameter
  theText            - the loaded field value
  theErrorStr        - error message
return:
  false on error
notes:
  - additional to LoadTextBlobField name of the
    primary key is needed
}

function LoadTextBlobField_Ex(const theTableName: String;
  const thePrimaryKeyName: String; thePrimaryKeyValue: Integer;
  const theFieldName: String; var theText: String;
  var theErrorStr: String): Boolean;
var
  FQuerySQL : TQuery;
  StringStream : TStringStream;
  Stream : TStream;
  bOK : Boolean;
begin
  bOK:= False;

  theErrorStr:= S_internal_error;

  theText:= '';

  try
    FQuerySQL := CreateQuery; // TQuery.Create ...
    StringStream := TStringStream.Create('');
    try
      with FQuerySQL do
      begin
        SQL.Add('Select '+theFieldName+' From '+theTableName);
        SQL.Add('Where '+thePrimaryKeyName+'=:Id');
        ParamByName('Id').AsInteger:= thePrimaryKeyValue;
        Open;
        try
          if QueryIsNotEmpty then
          begin
            Stream:= CreateBlobStream(FieldByName(theFieldName),bmRead);
            try
              StringStream.CopyFrom(Stream,0);
            finally
              Stream.Free;
            end;

            theText:= StringStream.DataString;

            bOK:= True;
          end;
        finally
          Close;
        end;
      end; { with FQuerySQL do }
    finally
      FQuerySQL.Free;
      StringStream.Free;
    end;
  except
    on E: Exception do
    begin
      theErrorStr:= E.message;
    end;
  end;

  Result:= bOK;
end; { LoadTextBlobField_Ex }
Heiko
  Mit Zitat antworten Zitat