FireDAC 学习 - 2

复制数据

ClientDataSet 依赖的是 Midas.dll,也就是说它是在 Midas 框架里面的。

要想把一个 ClientDataSet 的数据复制给另外一个 ClientDataSet,最简单的做法是:ClientDataSet2.Data := ClientDataSet1.Data;

这里的 Data 是一个 Variant 变量,它内含了具体的数据。

但是,一个普通的 DataSet 比如一个 TADOQuery 是没有 Data 这个属性的。它的数据要变为 ClientDataSet 可以接收的数据,需要通过 TDataSetProvider 来完成。这是 Midas 框架下的操作。

----------------------

在 FireDAC 下面,上一篇文章说了一个 FDQuery 可以将数据输出为 TMemory,然后让 FdMemTable.LoadFromMem获得。

其实这里有一个更简单的方法:

FdMemTable1.Data := FdQuery1.Data;  //测试通过。

这里,Data 是一个接口 IFDDataSetReference。

Delphi 官方 Help 如此描述:

Represents the data of the dataset, allowing to fully copy data from one dataset into another.

The Data property represents the dataset's internal in-memory data storage. Using this property, an application can copy the current structure and data of one FireDAC dataset to another FireDAC dataset. 

The property value is a reference to the IFDDataSet interface. It is reference-counted and the application does not need to explicitly free it. If the application keeps the interface reference using a variable or a field, then the reference must be released before the dataset is closed. 

The dataset must be inactive to set this property value, otherwise an exception is raised. After its setting, the Self dataset:

  • Has a structure of an original dataset, excluding Indexes, IndexDefs, Filter, and so on.
  • Has a copy of an original dataset data, including all row versions and states (inserted, deleted, updated, unchanged).
  • Has CachedUpdates equal to True, if an original dataset has unapplied changes.
  • Is open.

Example

FDQuery1.SQL.Text := 'select * from orders; select * from customers';

FDQuery1.Open;
FDQuery1.FetchAll;
// assign orders records to FDMemTable1
FDMemTable1.Data := FDQuery1.Data;

FDQuery1.NextRecordSet;
FDQuery1.FetchAll;
// assign customers records to FDMemTable2
FDMemTable2.Data := FDQuery1.Data;

猜你喜欢

转载自blog.csdn.net/pcplayer/article/details/106772392