我正在使用Delphi 5+ BDE + Oracle。我的职能如下:
class function TClientDataSetFactory.GetClientDataSet(
const qryGen: TDataSet): TClientDataSet;
var
dspDados: TDataSetProvider;
begin
Result := nil;
try
try
Result := TClientDataSet.Create(nil);
dspDados := TDataSetProvider.Create(Result);
dspDados.DataSet := qryGen;
qryGen.Active := True;
qryGen.First;
Result.Data := dspDados.Data;
Result.First;
except
on E: Exception do
begin
raise;
end;
end;
finally
end;
end;因此,当运行如下:
var
qryGen: TQuery;
cdsGen: TClientDataSet;
begin
qryGen := nil;
try
try
qryGen := CriaQuery();
qryGen.SQL.Text :=
'SELECT SUM(TOTAL) AS TOTAL FROM MYTABLE';
cdsGen := TClientDataSetFactory.GetClientDataSet(qryGen);
ShowMessageFmt('Total: %f', [cdsGen.FieldByName('TOTAL').AsFloat]);
except
on E: Exception do
begin
raise;
end;
end;
finally
if Assigned(qryGen) then FreeAndNil(qryGen);
end;
end;我有"159,00“,但是,如果我运行这个:
ShowMessageFmt('Total:%f',qryGen.FieldByName('TOTAL').AsFloat);
我得到了"159,25“
有人能帮我吗?
发布于 2014-04-25 14:00:58
我用另一个解决办法解决了这个问题。
type
TInternalQuery = class(TQuery)
protected
procedure InternalInitFieldDefs; override;
public
constructor Create(AOwner: TComponent; const qryGen: TQuery); reintroduce;
end;
constructor TInternalQuery.Create(AOwner: TComponent; const qryGen: TQuery);
var
intCont: Integer;
begin
inherited Create(AOwner);
Self.DatabaseName := qryGen.DatabaseName;
Self.UpdateObject := qryGen.UpdateObject;
Self.SQL.Text := qryGen.SQL.Text;
for intCont := 0 to Self.ParamCount - 1 do
begin
Self.Params[intCont].Value := qryGen.Params[intCont].Value;
end;
end;
procedure TInternalQuery.InternalInitFieldDefs;
var
intCont: Integer;
begin
inherited InternalInitFieldDefs;
for intCont := 0 to FieldDefs.Count - 1 do
begin
if (FieldDefs[intCont].Size = 0) and (FieldDefs[intCont].DataType = ftBCD) then
begin
FieldDefs[intCont].Precision := 64;
FieldDefs[intCont].Size := 32;
end;
end;
end;问题是((FieldDefsintCont.Size = 0)和(FieldDefsintCont.DataType = ftBCD))。当创建ClientDataSet时,字段会被截断,因为当oracle具有类似于"SUM(TOTAL)“的函数时,结果字段将被创建为0大小,因此clientdataset将该字段处理为Integer字段。
发布于 2014-03-13 18:49:59
试着
ShowMessageFmt('Total: %n', [cdsGen.FieldByName('TOTAL').AsFloat])
或者这个
cdsGen := TClientDataSetFactory.GetClientDataSet(qryGen); **(cdsGen.FieldByName('Total') as TFloatField).DisplayFormat := '0.00';** ShowMessageFmt('Total: %f', [cdsGen.FieldByName('TOTAL').AsFloat])
https://stackoverflow.com/questions/22303298
复制相似问题