query,clientdataset均可以动态创建
也可以重ADOquery 得到不过得注意DataType
如果不动结构和数据:
TmpClientDataSet:=TClientDataSet.create;
TmpQuery:=TQuery.create;
TmpQuery.sql.add('select...');
Tmpquery.open;
TmpclentDataSet.data:=TmpQuery.Get_data;
TmpQuery.free;
....
***********************
如果要修改数据和结构如下比较复杂,给一个过滤多对多查重例子:
第一步按Query创将TClientDataSet的结构
TmpClientDataSet:=TClientDataSet.create;
TmpQuery:=TQuery.create;
with TmpClientDataSet do
begin
for i:= 0 to TmpQuery.fieldcount -1 do
begin
if (TmpQuery.fields.datatype = ftstring) or
(TmpQuery.fields.datatype = widestring)
then fielddefs.add(TmpQuery.fields.fieldname,
TmpQuery.fields.datatype,TmpQuery.fields.size,False)
else if TmpQuery.Fields.DataType = ftAutoInc then
fielddefs.add(TmpQuery.fields.fieldname,ftInteger,0,False)
else fielddefs.add(TmpQuery.fields.fieldname,TmpQuery.fields.datatype,0,False);
end;
TmpClientDataSet.createdataset;
end;
第二步过滤数据并给TClientDataSet付值
with TmpQuery do
begin
first;
SetLength(ADDValues, fieldcount );
KeyValues:= FieldValues[keyFieldname.name];
for i:=0 to fieldcount -1 do
ADDValues:= fields.AsVariant;
next;
while not eof do
begin
if KeyValues = FieldValues[keyFieldname.name] then
begin
for p:=0 to k-1 do
if Pos(FieldValues[ADDFieldname[p].name],ADDValues[ADDFieldname[p].index]) <= 0 then
ADDValues[ADDFieldname[p].index]:=ADDValues[ADDFieldname[p].index]+ FieldValues[ADDFieldname[p].name];
end else
begin
TmpClientDataSet.append;
for i:= 0 to TmpClientDataSet.fieldcount -1 do TmpClientDataSet.fields.AsVariant:= ADDValues;
for i:=0 to fieldcount -1 do ADDValues:= fields.AsVariant;
KeyValues:= FieldValues[keyFieldname.name];
end;
next;
end;
TmpClientDataSet.append;
for i:= 0 to TmpClientDataSet.fieldcount -1 do TmpClientDataSet.fields.AsVariant:= ADDValues;
result:=TmpClientDataSet.Data;
TmpClientDataSet.Free;
end;
end;