一次得到多个数据集
function GetDataSetsByJson(SQL_Statement : string) : TFDJSONDataSets; //得到多个数据集 server
{SQL_Statement格式 'Name' : '名称','CommandText':'SQL语句' } var jo : TJSONObject; jp : TJSONPair; i : Integer; Arr: TArr<TFDQuery>; begin {$REGION '一次得到多个数据集'} jo := TJSONObject.ParseJSONValue(Trim(SQL_Statement),True) as TJSONObject; if not Assigned(jo) then Result := nil; try Result := TFDJSONDataSets.Create; Setlength(Arr,jo.Count); for i := 0 to jo.Count - 1 do begin jp := jo.Pairs[i]; Arr[i] := TFDQuery.Create(Self); Arr[i].Connection := FDConnection1; Arr[i].SQL.Clear; Arr[i].SQL.Add(jp.JsonValue.Value); Arr[i].open; TFDJSONDataSetsWriter.ListAdd(Result,jp.JsonString.Value,Arr[i]); end; finally jo.Free; end; {$ENDREGION} end;
调用
function TDM.getTables_Json(ArraySQL: array of string; aFDMemTables: array of TFDMemTable) : Boolean; //client var aServer: TUsegearClient; LDataSetList: TFDJSONDataSets; aDataSet: TFDAdaptedDataSet; jp : TJSONPair; jo : TJSONObject; i,j : Integer; begin {$REGION '一次得到多个数据集'} Result := False; if (Length(arraySQL) <> Length(aFDMemTables)) then begin raise Exception.Create('数组数不一致,请检查'); Exit end; aServer := TUsegearClient.Create(DSRestConnection1); try jo := TJSONObject.Create; for I := Low(ArraySQL) to High(ArraySQL) do //准备sql statement begin jp := TJSONPair.Create('T'+ i.ToString,ArraySQL[i]); jo.AddPair(jp); end; try LDataSetList := aServer.GetDataSetsByJson(jo.ToString); ShowMessage(TFDJSONDataSetsReader.GetListCount(LDataSetList).ToString); j := TFDJSONDataSetsReader.GetListCount(LDataSetList); for I := 0 to j - 1 do begin aDataSet := TFDJSONDataSetsReader.GetListValueByName(LDataSetList, 'T'+ i.ToString); aFDMemTables[i].Close; aFDMemTables[i].AppendData(aDataSet); end; Result := True; except Result := False; end; finally jo.Free; aServer.Free; end; {$ENDREGION} end;
if ClientDMUnit.Dm.getTables_Json(['select * from ProductInfo','select * from ProductParams'],[u3FDMemTable,u2FDMemTable]) then //这里在程序中调用 begin u3FDMemTable.Open; u2FDMemTable.Open; end;