UniDAC 的 RecordCount 属性注意事项

一直以来都是用UniDAC作为数据库查询组件,而一般的写法都是这样:

UniQuery_sqlite.close;
UniQuery_sqlite.SQL.Clear;
UniQuery_sqlite.SQL.Add('SELECT * FROM TABLE');
try
  UniQuery_sqlite.Open;
except
  { TODO : 异常捕获 }
end;
//循环出所有记录
for i := 0 to UniQuery_sqlite.RecordCount - 1 do
begin
   //....
   UniQuery_sqlite.Next;
end;

今天突然发现一个问题,明明数据库里存在近百条记录,但只查出来了25条,仔细检查了代码,没有问题啊,于是又检查了UniDAC UniQuery的属性,发现有这样一个属性: FetchRows,而且默认值是25!肯定是这里的问题了,帮助里搜了一下这个属性,帮助解释如下:

可见这个属性是为了性能考虑的,既然默认设置成25,肯定是最佳设置了,还是不改为何,于是寻求其他解决办法,网上又搜到了这样一个解释:
If you set the QueryRecCount option to True, TUniTable executes SELECT COUNT() query automatically when you open the table, and assigns the correct value to the RecordCount property.
于是,我试着把QueryRecCount属性设置为true,问题解决了,showmessage了一下recordcount,不再是25了,已经是正确的行数了。为了找到权威的解释,又搜了一下帮助文档,解释如下:

特别是这一句:Used for TCustomDADataSet to perform additional query to get the record count for this SELECT, so the RecordCount property reflects the actual number of records.

原文地址:https://www.cnblogs.com/chenmfly/p/4818373.html