[英]TDataset and TMemDataset
I need to iterate through a number of MySQL queries and save them in an array of TMemDataset
's. 我需要遍历一些MySQL查询并将它们保存在TMemDataset
的数组中。 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"). 但是,每次查询更改时,所有以前的TMemDataset
更改为包含新值(我猜因为它们是“数据感知组件”)。 If I get rid of ZQuery1
with ZQuery1.Free
, then all of the data vanishes. 如果我摆脱ZQuery1
与ZQuery1.Free
,那么所有的数据消失。 How do I avoid this? 我该如何避免这种情况?
I am using FreePascal, but I bet the solution for Delphi would apply too. 我正在使用FreePascal,但我敢打赌Delphi的解决方案也适用。
The solution is to have an array of ZQuery
as well as an array of MemDataSet
解决方案是拥有一个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;
Something like that should work, because now there's no more confusion between the Queries and the memdatasets. 这样的东西应该有效,因为现在查询和memdatasets之间不再混淆了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.