TDataset和TMemDataset

时间:2011-11-20 07:43:28

标签: mysql delphi dataset freepascal zeos

我需要迭代一些MySQL查询并将它们保存在TMemDataset的数组中。这似乎是这样做的:

MemDataset1.CopyFromDataset(ZQuery1,True); 

然而,每次查询更改时,所有先前的TMemDataset都会更改为包含新值(我猜是因为它们是“数据感知组件”)。如果我用ZQuery1删除ZQuery1.Free,那么所有数据都会消失。我该如何避免这种情况?

我正在使用FreePascal,但我敢打赌Delphi的解决方案也适用。

1 个答案:

答案 0 :(得分:1)

解决方案是拥有一个ZQuery数组以及一个MemDataSet数组

type
  TZQueries = array of TZQuery;
  TMemDataSets = array of TMemDataset;

procedure Test;
var
  ZQueries: TZQueries;
  MemDatasets: TMemDatasets;
  i: integer;
begin
  try
    SetLength(ZQueries,10);
    SetLength(MemDatasets,10);
    for i:= Low(ZQueries) to high(ZQueries) do begin
      ZQueries[i]:= TZQuery.Create;
      ZQueries[i].Connection:= ZConnection1;
      ZQueries[i].SQL.Text:= QueryTextFromSomewhere
    end; {for i} 
    for i:= Low(MemDatasets) to High(MemDatasets) do begin
      MemDatasets[i]:= TMemDataset.Create;
      ZQueries[i].Open;
      MemDatasets[i].CopyFromDataset(ZQueries[i],True);
    end; {for i}
    ....
      code to process the memdatasets
    ....
  finally
    for i = Low(ZQueries) to High(ZQueries) do begin
      ZQueries[i].Free;
    end; {for i}
    for i = Low(MemDatasets) to High(MemDatasets) do begin
      MemDatasets[i].Free;
    end; {for i}
  end; {tryf}
end;

这样的东西应该可以工作,因为现在查询和memdatasets之间不再混淆。