【问题标题】:ClientDataSet TBCDField roundingClientDataSet TBCDField 舍入
【发布时间】:2014-03-10 14:42:19
【问题描述】:

我正在使用 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”。

有人可以帮我吗?

【问题讨论】:

  • 嗯...也许您需要在运行查询之前将 cdsGen“Total”字段定义为浮点 (ftFloat) 类型。
  • 如果我定义为 ftFloat,我得到类型不匹配,预期 Float 并得到 BCD

标签: oracle delphi tclientdataset


【解决方案1】:

我用另一个解决方案解决了这个问题。

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;

问题是 ((FieldDefs[intCont].Size = 0) 和 (FieldDefs[intCont].DataType = ftBCD))。当创建ClientDataSet时,该字段被截断,因为当oracle具有“SUM(TOTAL)”之类的函数时,结果字段的大小为0,因此clientdataset将该字段作为整数字段处理。

【讨论】:

    【解决方案2】:

    试试

    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])

    【讨论】:

    • 问题在于值,而不是表示 cdsGen.FieldByName('TOTAL').AsFloat = 159.00 和 qryGen.FieldByName('TOTAL').AsFloat = 159.25
    猜你喜欢
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 2012-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多