下面代码可以在TQuery中增加字符类字段,怎样增加integer\Currency类字段?请高手出手!!!谢谢procedure AddDataField(QryR:TQuery;FldName:string);
var t:TStringField;
begin
  if QryR.Fields.FindField(FldName)<>nil then Exit;
  QryR.Close;
  t:=TStringField.Create(QryR);
  t.FieldKind := fkData;
  t.DisplayLabel := FldName;             //新建字段显示名
  t.FieldName := FldName;                //新建字段数据名
  t.DataSet := QryR;                     //新建字段数据名
  t.Size := 50;                          //新建字段字符数
  t.AsString;
end;

解决方案 »

  1.   

    非要在程序中加吗?那你还不如采用alter table语句呢
      

  2.   

    procedure AddDataField(MyFieldInfo:TFieldInfo);
    var t1:TStringField;
        t2:TIntegerField;
        t3:TCurrencyField;
    begin
      if MyFieldInfo.QryName.Fields.FindField(MyFieldInfo.FldName)<>nil then Exit;
      if MyFieldInfo.DataType='char' then
      begin
       t1:=TStringField.Create(MyFieldInfo.QryName);
       t1.FieldKind := fkData;
       t1.DisplayLabel := MyFieldInfo.FldName;   //新建字段显示名
       t1.FieldName := MyFieldInfo.FldName;      //新建字段数据名
       t1.DataSet := MyFieldInfo.QryName;        //新建字段数据名
       t1.SetFieldType(ftString);                //字符型
       t1.Size := MyFieldInfo.FLdWidth;          //新建字段字符数
      end;
      if MyFieldInfo.DataType='int' then
      begin
       t2:=TIntegerField.Create(MyFieldInfo.QryName);
       t2.FieldKind := fkData;
       t2.DisplayLabel := MyFieldInfo.FldName;   //新建字段显示名
       t2.FieldName := MyFieldInfo.FldName;      //新建字段数据名
       t2.DataSet := MyFieldInfo.QryName;        //新建字段数据名
       t2.SetFieldType(ftinteger);               //整型
      end;
      if MyFieldInfo.DataType='money' then
      begin
       t3:=TCurrencyField.Create(MyFieldInfo.QryName);
       t3.FieldKind := fkData;
       t3.DisplayLabel := MyFieldInfo.FldName;   //新建字段显示名
       t3.FieldName := MyFieldInfo.FldName;      //新建字段数据名
       t3.DataSet := MyFieldInfo.QryName;        //新建字段数据名
       t3.SetFieldType(ftCurrency);              //货币型
       t3.DisplayFormat:='###########0.00';      //显示格式
      end;
    end;
      

  3.   

    //数据字段信息
    Type
     TFieldInfo=record
      QryName:TQuery;    // 数据集名
      FldName:String;    // 字段名
      DataType:String;   // 数据类型
      FLdWidth:integer;  // 字段宽度
    end;