Convert ftFloat to ftBCD data type

How to convert field type from ftFloat to ftBCD;

I tried

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;

But I get an error

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

Is there a way to convert the entire dataset field that ftFloat to ftBCD?

+4
source share
3 answers

DataType is readonly A property of the Tfield created for DataType. This is done from Fielddefs using DefaultFieldClasses: array [TFieldType] from TFieldClass from the database. If you need to change the DataType, you will need to free the field and create another one to suit your needs. The following shows how to do this.

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;
+6
source

Here is another way:

First you need to upload the table to a file like this

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');
+1

! , ! , Filedtype , enum. : TintegerField ..

, FieldType , TList

?

0

All Articles