Delphi:使用数据库中的BigInts

时间:2021-06-30 16:48:20

I´m using Delphi 7 with devart dbExpress to connect to SQLServer. The problem is that when I add a bigInt field to a ClientQuery it comes as TFMTBCDField.

我使用Delphi 7和devart dbExpress连接到SQLServer。问题是当我向ClientQuery添加一个bigInt字段时,它就像TFMTBCDField一样。

And the TFMTBCDField don´t have a method to get the 64 bit value.

并且TFMTBCDField没有获取64位值的方法。

I can use the Field.AsVariant or the StrToInt64(Field.AsString) to pick this 64 bits value.

我可以使用Field.AsVariant或StrToInt64(Field.AsString)来选择这个64位值。

Is there a better way to pick/use this value?

是否有更好的方法来选择/使用此值?

4 个解决方案

#1


4  

Maybe add a TLargeIntField manualy to dataset, set it's FieldName to appropriate name and use such code:

也许可以将TLargeIntField手动添加到数据集中,将其FieldName设置为适当的名称并使用以下代码:

SomeInt64Value := (qryMyQuery.FieldByName('blahblah') as TLargeIntField).AsLargeInt;

Do not remember exactly types, but it worked this way in Delphi6.

不记得确切的类型,但它在Delphi6中以这种方式工作。

#2


0  

You can convert the BCD to Variant and than to int64 with VarFMTBcdCreate from unit FMTBcd.

您可以使用单元FMTBcd中的VarFMTBcdCreate将BCD转换为Variant而不是int64。

Try this:

尝试这个:

var value64 : int64;
...
value64 := VarFMTBcdCreate(Field.Value);

#3


0  

The data format for TFMTBCDField is the TBcd record from the FMTBcd unit. You can get that raw value by reading the field's Value or AsBCD properties.

TFMTBCDField的数据格式是FMTBcd单元的TBcd记录。您可以通过读取字段的Value或AsBCD属性来获取该原始值。

Depending on what you need the value for, TBcd might be sufficient. That is, you might not need to convert it to an Int64. The FMTBcd unit provides functions to add, subtract, multiply, and divide TBcd values.

根据您需要的值,TBcd可能就足够了。也就是说,您可能不需要将其转换为Int64。 FMTBcd单元提供添加,减去,乘法和除法TBcd值的函数。

The unit provides no conversions to Int64. There are conversions to Variant, string, Currency, Double, and Integer. If we were going to write an Int64 conversion, the Integer conversion is probably a good place to start, so let's take a look at how it's implemented:

该单元不提供Int64的转换。转换为Variant,string,Currency,Double和Integer。如果我们要编写一个Int64转换,整数转换可能是一个很好的起点,所以让我们来看看它是如何实现的:

function BcdToInteger(const Bcd: TBcd; Truncate: Boolean = False): Integer;
var
  ABcd: TBcd;
begin
  if Truncate and (BcdScale(Bcd) > 0) then
    NormalizeBcd(Bcd, ABcd, Bcd.Precision, 0)
  else
    ABcd := Bcd;
  Result := StrToInt(BcdToStr(ABcd));    
end;

So, the VCL itself doesn't provide any more direct way to convert a TBcd to an Integer than to go through string. Therefore, it looks like your idea to call StrToInt64 on the string version of the field is fine.

因此,VCL本身不提供将TBcd转换为整数而不是通过字符串的更直接方式。因此,看起来你的想法是在字段的字符串版本上调用StrToInt64很好。

#4


-1  

I dont have Delphi 7 installed here anymore, but looking in the help, I see you can get as Float (Double), like this:

我不再安装Delphi 7了,但是在帮助中,我看到你可以得到Float(Double),像这样:

function GetFieldAsInt64(Field: TField): Int64;
begin
  Result:= Int64(Round(Field.GetAsFloat));
end;

And then, call the function:

然后,调用函数:

var
  Value: Int64;
begin
  Value:= GetFieldAsInt64(MyFMTBCDField);

end;

#1


4  

Maybe add a TLargeIntField manualy to dataset, set it's FieldName to appropriate name and use such code:

也许可以将TLargeIntField手动添加到数据集中,将其FieldName设置为适当的名称并使用以下代码:

SomeInt64Value := (qryMyQuery.FieldByName('blahblah') as TLargeIntField).AsLargeInt;

Do not remember exactly types, but it worked this way in Delphi6.

不记得确切的类型,但它在Delphi6中以这种方式工作。

#2


0  

You can convert the BCD to Variant and than to int64 with VarFMTBcdCreate from unit FMTBcd.

您可以使用单元FMTBcd中的VarFMTBcdCreate将BCD转换为Variant而不是int64。

Try this:

尝试这个:

var value64 : int64;
...
value64 := VarFMTBcdCreate(Field.Value);

#3


0  

The data format for TFMTBCDField is the TBcd record from the FMTBcd unit. You can get that raw value by reading the field's Value or AsBCD properties.

TFMTBCDField的数据格式是FMTBcd单元的TBcd记录。您可以通过读取字段的Value或AsBCD属性来获取该原始值。

Depending on what you need the value for, TBcd might be sufficient. That is, you might not need to convert it to an Int64. The FMTBcd unit provides functions to add, subtract, multiply, and divide TBcd values.

根据您需要的值,TBcd可能就足够了。也就是说,您可能不需要将其转换为Int64。 FMTBcd单元提供添加,减去,乘法和除法TBcd值的函数。

The unit provides no conversions to Int64. There are conversions to Variant, string, Currency, Double, and Integer. If we were going to write an Int64 conversion, the Integer conversion is probably a good place to start, so let's take a look at how it's implemented:

该单元不提供Int64的转换。转换为Variant,string,Currency,Double和Integer。如果我们要编写一个Int64转换,整数转换可能是一个很好的起点,所以让我们来看看它是如何实现的:

function BcdToInteger(const Bcd: TBcd; Truncate: Boolean = False): Integer;
var
  ABcd: TBcd;
begin
  if Truncate and (BcdScale(Bcd) > 0) then
    NormalizeBcd(Bcd, ABcd, Bcd.Precision, 0)
  else
    ABcd := Bcd;
  Result := StrToInt(BcdToStr(ABcd));    
end;

So, the VCL itself doesn't provide any more direct way to convert a TBcd to an Integer than to go through string. Therefore, it looks like your idea to call StrToInt64 on the string version of the field is fine.

因此,VCL本身不提供将TBcd转换为整数而不是通过字符串的更直接方式。因此,看起来你的想法是在字段的字符串版本上调用StrToInt64很好。

#4


-1  

I dont have Delphi 7 installed here anymore, but looking in the help, I see you can get as Float (Double), like this:

我不再安装Delphi 7了,但是在帮助中,我看到你可以得到Float(Double),像这样:

function GetFieldAsInt64(Field: TField): Int64;
begin
  Result:= Int64(Round(Field.GetAsFloat));
end;

And then, call the function:

然后,调用函数:

var
  Value: Int64;
begin
  Value:= GetFieldAsInt64(MyFMTBCDField);

end;