How to get a TDBXTypes.BLOB field value?

How to get a TDBXTypes.BLOB field value?

Postby fabio_rbs » Thu 26 May 2011 02:27

Hi,

I have a "BLOB SUB_TYPE 0" field that is returned as TDBXTypes.BLOB type, this field stores only text files. (I know it would be better if it were "SUB_TYPE 1", but it's "SUB_TYPE 0")

How can I get the field value and put it on a string ?

I tried with .GetAnsiString, .GetString, .GetWideString, and the error below occurs:
"TDBXTypes.BLOB value type cannot be accessed as TDBXTypes.WideString value type."

Please, see the code:

Code: Select all
procedure TFrmPrincipal.BitBtnTestClick(Sender: TObject);
var
  DBXTrn: TDBXTransaction;
  DBXCmd: TDBXCommand;
  DBXRdr: TDBXReader;
  sTxt: string;
begin
  sTxt := '';
  DBXTrn := DM.DBXCon.DBXConnection.BeginTransaction(TDBXIsolations.ReadCommitted);
  try
    DBXCmd := DM.DBXCon.DBXConnection.CreateCommand;
    try
      DBXCmd.Text := 'SELECT FIRST 1 XMLFILE FROM T_XMLTBL';
      DBXCmd.Prepare;
      DBXRdr := DBXCmd.ExecuteQuery;
      if DBXRdr.Next then
        sTxt := DBXRdr.Value['XMLFILE'].GetString;
      DBXRdr.Free;
    finally
      DBXCmd.Free;
    end;

    DM.DBXCon.DBXConnection.CommitFreeAndNil(DBXTrn);
  except
    DM.DBXCon.DBXConnection.RollBackFreeAndNil(DBXTrn);
    raise;
  end;
  ...
  ...
end;


I'm using Delphi 2010, Firebird 2.1.4 and DbxIda 2.70.28.

Thanks,

Fabio R. Bot da Silva.
fabio_rbs
 
Posts: 9
Joined: Wed 27 Oct 2010 23:19

Postby AndreyZ » Thu 26 May 2011 11:00

Hello,

You can use the following code:
Code: Select all
procedure TFrmPrincipal.BitBtnTestClick(Sender: TObject);
var
  DBXTrn: TDBXTransaction;
  DBXCmd: TDBXCommand;
  DBXRdr: TDBXReader;
  sTxt: string;
  str: TStream;
  bytes: TBytes;
begin
  sTxt := '';
  DBXTrn := DM.DBXCon.DBXConnection.BeginTransaction(TDBXIsolations.ReadCommitted);
  try
    DBXCmd := DM.DBXCon.DBXConnection.CreateCommand;
    str := nil;
    try
      DBXCmd.Text := 'SELECT FIRST 1 XMLFILE FROM T_XMLTBL';
      DBXCmd.Prepare;
      DBXRdr := DBXCmd.ExecuteQuery;
      if DBXRdr.Next then begin
        str := DBXRdr.Value['XMLFILE'].GetStream;
        SetLength(bytes, str.Size);
        str.ReadBuffer(bytes[0], str.Size);
        sTxt := StringOf(bytes);
      end;
      DBXRdr.Free;
    finally
      SetLength(bytes, 0);
      str.Free;
      DBXCmd.Free;
    end;

    DM.DBXCon.DBXConnection.CommitFreeAndNil(DBXTrn);
  except
    DM.DBXCon.DBXConnection.RollBackFreeAndNil(DBXTrn);
    raise;
  end;
  ...
  ...
end;
AndreyZ
 

Postby fabio_rbs » Thu 26 May 2011 13:39

Your code works fine.

Thanks a lot.

Fábio.
fabio_rbs
 
Posts: 9
Joined: Wed 27 Oct 2010 23:19


Return to dbExpress driver for InterBase & Firebird