4

如何将字段类型从 ftFloat 转换为 ftBCD;

我试过

for i := 0 to FDataSet.FieldCount - 1 do begin
      if FDataSet.Fields.Fields[i].DataType = ftFloat then begin
           FDataSet.Fields.Fields[i].DataType := ftBCD;
      end;
end;

但我得到了错误

[DCC Error]  E2129 Cannot assign to a read-only property

有没有办法可以将 ftFloat 的所有数据集字段转换为 ftBCD ?

4

3 回答 3

7

DataType 是为 DataType 创建的 Tfield 的只读属性。这是从 Fielddefs 使用 DefaultFieldClasses: array[TFieldType] of TFieldClass from DB 完成的。如果您需要更改 DataType,您将必须释放该字段并创建另一个适合您需要的字段。下面显示了如何做到这一点的示例。

type
  TMyFieldInfo = Record
    FieldName: String;
    Size: Integer;
    DataType: TFieldType;
    FieldKind: TFieldKind;
  end;

type
  TFA= Array of TMyFieldInfo;

 Procedure GetFields(DS:Tdataset;var FA:TFA);
  var
    I: Integer;
  begin
    SetLength(FA, DS.FieldCount);
    for I := 0 to DS.FieldCount - 1 do
    begin
      FA[I].FieldName := DS.Fields[I].FieldName;
      FA[I].DataType := DS.Fields[I].DataType;
      FA[I].Size := DS.Fields[I].Size;
      FA[I].FieldKind := fkdata;
    end;
  end;

  Procedure SetFields(DS:Tdataset;var FA:TFA);
  var
    I: Integer;
    F:TField;
  begin
    DS.Fields.Clear;
    for I := Low(FA) to High(FA) do
    begin
      F := DefaultFieldClasses[FA[I].DataType].Create(DS);
      With F do
      begin
        FieldName := FA[I].FieldName;
        FieldKind := FA[I].FieldKind;
        Size := FA[I].Size;
        DataSet := DS;
      end;
    end;

  end;


procedure TForm6.Button1Click(Sender: TObject);
var
   L_FA: TFA;
   I:Integer;
begin
    MyDS.Open;  // open to get the Fielddefs.
    GetFields(MyDS,L_FA);
    MyDS.Close;  // close to be able to change the fields
    for I := Low(L_FA) to High(L_FA) do
      begin
         if L_FA[i].DataType = ftFloat then
            L_FA[i].DataType := ftBCD;
      end;
    SetFields(MyDS,L_FA);
    MyDS.Open;
end;
于 2014-09-11T08:40:12.693 回答
1

这是另一种方式:

首先,您需要将表转储到这样的文件中

ADOQuery.SaveToFile('C:\1.xml');

然后在其中找到您的字段描述,假设它将是这样的:

<s:datatype dt:type='float' dt:maxLength='8' rs:fixedlength='true' rs:maybenull='true'/>

并将其替换为其他类型描述,如下所示:

<s:datatype dt:type='number' rs:dbtype='currency' dt:maxLength='25' rs:precision='25' rs:fixedlength='true' rs:maybenull='true'/>

现在您需要重新加载此文件,如下所示:

ADOQuery.LoadFromFile('C:\1.xml');
于 2014-12-01T02:27:19.223 回答
0

不!创建数据字段后,您将无法更改它!这是因为分配 Filedtype 不仅仅是更改枚举类型属性。每个字段类型都是一个特定的类:TintegerField 等...

因此,您无法更改 FieldType 的原因与无法将 TList 转换为字符串的原因相同

你到底想做什么?

延斯·鲍里斯霍尔特

于 2014-09-11T07:21:21.780 回答