array of TVarRec 动态数组使用

FDQuery.AppendRecord()里是一个array of TVarRec。我们一般都是直接用[Var1,Var2,...]。这样手工输入,但如果增加的元素我们预先不知道,就要声明一个array of TVarRec的动态数组,然后赋值了。我一直找不到方面,经QDAC的作者指点。做了以下实例:

procedure TForm1.btn1Click(Sender: TObject);
var
  cSQL:string;
  cList:TStringList;
  cRec:array of TVarRec;
  cText:Variant;
  i:Integer;
begin
  cList:=TStringList.Create;
  cList.Add('01001');
  cList.Add('原材料');
  SetLength(cRec,cList.Count);
  for I := 0 to cList.Count-1 do
    begin
      cText:=cList.Strings[i];
      cRec[i+1].VVariant:=Addr(cText);
    end;

 // FDQuery1.AppendRecord(cRec);
end;

 XE10后发生了一些变成,不知是什么原因原来的代码不可以用了。改了一下代码。

procedure TForm3.SpeedButton1Click(Sender: TObject);
var
  cList:TStringList;
  cRec:array of TVarRec;
  i:Integer;
begin
  if not  FDMemTable1.Exists then  FDMemTable1.CreateDataSet;
  cList:=TStringList.Create;
  cList.Add('01001');
  cList.Add('原材料');
  SetLength(cRec,cList.Count);
  for I := 0 to cList.Count-1 do
    begin
      cRec[i].VAnsiString:=Pointer(cList.Strings[i]);
      cRec[i].VType:=17;
    end;
  cList.Free;
  Self.FDMemTable1.AppendRecord(cRec);
end;

 

posted @ 2014-12-19 20:49  似水@流年  阅读(1418)  评论(0编辑  收藏  举报
悠悠记得当天笑 仿佛入迷 又带一点惘 种种喜悦 令人为你鼓掌 眉飞色舞千千样 你是个妙人 是个少年狂