Conversion Bit to String

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
invent
Posts: 92
Joined: Tue 16 Jun 2009 10:59
Location: Bielefeld, Germany

Conversion Bit to String

Post by invent » Wed 10 Oct 2018 16:35

Hi,

i have a question to use Bit-Fields with Delphi 7 / UniDAC 4.5.9 on MS Sql-Server 2012:

I created a table:

Code: Select all

CREATE TABLE [dbo].[inv_WWS_Activity](
	[RecId] [char](32) NOT NULL,
	[inv_IsBilled] [bit] NULL, 
	...
The Datafield "inv_IsBilled" contains 0 or 1. Then I use TUniQuery:

Code: Select all

select RecId, inv_IsBilled 
from inv_WWS_Activity'
where RECID = '12345'
I get a record and when I show the content of the bit-field with

Code: Select all

ShowMessage (MyQuery.FieldByName ('inv_IsBilled').AsString);
The result is "Wahr" (in englisch: true). But i expect '0' or '1'.

What can i do? Do I have to change an option?

Thanks in advance for any help.

Kind regards,
Gerd Brinkmann
invent GmbH

frickler
Posts: 6
Joined: Wed 04 Apr 2018 08:30

Re: Conversion Bit to String

Post by frickler » Mon 15 Oct 2018 07:21

Looks like datatype "bit" is mapped to "boolean". Which seems to make sense in your case, since "is_billed" is either true or false. How to change that mapping is described in the help file under "Using UniDAC - Data Type Mapping".

Stellar
Devart Team
Posts: 183
Joined: Tue 03 Oct 2017 11:00

Re: Conversion Bit to String

Post by Stellar » Tue 16 Oct 2018 14:35

By default, the Bit data type in the database is mapped to the ftBoolean type in Delphi. If you want the Bit data type to be represented as a number, you can use DataTypeMapping, a special mechanism provided by our components. To set the mapping for a field, you can use the design time editor of the TMSQuery component (or globally in TMSConnection) on the Data type mapping tab you need to add a rule:
Database Type = Bit
Field Type = Byte

The type mapping rule can also be set programmatically, for example:

Code: Select all

uses
  MSDataTypeMap;

...

MSQuery1.DataTypeMap.AddDBTypeRule(msBit, ftByte);
You can also add a rule for a specific field, for example:

Code: Select all

MSQuery1.DataTypeMap.AddFieldNameRule('inv_IsBilled', ftByte, True);

Post Reply