TDataset 和 TMemDataset

发布于 2024-12-17 05:15:38 字数 332 浏览 1 评论 0原文

我需要迭代多个 MySQL 查询并将它们保存在 TMemDataset 数组中。这似乎可以做到这一点:

MemDataset1.CopyFromDataset(ZQuery1,True); 

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

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

I need to iterate through a number of MySQL queries and save them in an array of TMemDataset's. This seems to do it:

MemDataset1.CopyFromDataset(ZQuery1,True); 

However each time the query changes, all the previous TMemDataset's are changed to contain the new values (I guess because they are "data-aware components"). If I get rid of ZQuery1 with ZQuery1.Free, then all of the data vanishes. How do I avoid this?

I am using FreePascal, but I bet the solution for Delphi would apply too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡大本事 2024-12-24 05:15:38

解决方案是拥有一个 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;

类似的东西应该可以工作,因为现在查询和内存数据集之间不再存在混淆。

The solution is to have an array of ZQuery as well as an array of 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;

Something like that should work, because now there's no more confusion between the Queries and the memdatasets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文