Allen 2009-06-10 16:41:30 10438 1 0 0 0
Delphi技巧,TTTBLOG,Delphi查询返回的recordcount=-1的解决办法---20090521153400---有时,查询返回的recordcount=-1,总是-1,即使有记录也是这样,导致程序无法根据reco…

Delphi查询返回的recordcount=-1的解决办法
---
20090521 153400
---
有时,查询返回的recordcount=-1,总是-1,即使有记录也是这样,导致程序无法根据recordcount结果做出正确的反应。
原因不明,解决方法是:先定位到查询的开始,然后定位到最后,再判断recordcount。
如下:
    With dm.QR_ENNSHD_020 do begin
        close;
        ParamByName(^NO_STD_NO^).AsString:=edtNo_Std_No.Text;
        paramByName(^VER_NO^).AsString:=edtVer_No.Text;
        open;
        first;
        last;
        if recordcount<=0 then begin
            x_pMsg:=^Data no found!^;
            MessageDlg(x_pMsg,mtInformation,[mbOk],0);
            exit;
        end;


Tag: Delphi技巧 TTTBLOG
我也要发一个   ·   返回首页   ·   返回[Delphi]   ·   前一个   ·   下一个
评论
Allen#1Allen 2021-12-27 16:40:57(N) 链接地址

  ...

  Query.RecordCount返回-1是因为你的SQL语句返回的字段是有Blob字段靠造成的。

  其實數值型的也不行,我試過盡量SQL語句中不要用

  ...

  query.Last;

  query.RecordCount ;

  query.first;

  即可以得到记录数

  ...

  这不是BUG!!!小弟我也遇到过!是游标的问题!!!

  ...

  这是BDE的BUG,你可以用Select cc=COUNT(*) ,不要用Recordcount

  ...

  同意stonegem(啥都不懂,啥都想学) 的说法,如果包含Blob字段,Query.RecordCount就置为-1;

  TClientDataSet是有此功能,可以预先定义一次返回的记录数,但TQuery好像没有这个功能。

  建议用while not query1.eof do 或if not query1.eof then。

  ...

  housisong(侯子) 老兄的观点同意;

  简单的办法就是在recordcount前

  加.first和.last。

  ...

  其实这不是Delphi的bug,在一次查询显示中,如果数据记录数目大于能显示的数目(DBGrid)时,就会Query.recordcount=-1,这是数据并没有完全载入本地,当程序操作数据时,才会继续从数据库载入;所以这只是Delphi数据查询的优化。

  要得到准确的记录数你可以采用使用两种方法:1。Query.last;Query.first,然后Query.recordcount就可以得到正确值(数据很大时这样做效率很低);2。新增一条查询语句,用以统计记录数目。

  ...

  可以通过程序调出

  while not query1.eof do

  recordcount:=recordcount+1

  ...

  Query.RecordCount返回-1是因为你的SQL语句返回的字段是有Blob字段靠造成的。

  ...

  TBDEDataSet(RecordCount,RecNo仅对本地数据库有效)

  Note:

  Use RecordCount with care, because record counting can be a costly operation, especially for SQL queries that return large result sets. Generally, an application should only use RecordCount with Paradox and dBASE tables.

  Note:

  When the dataset has a filter or a range, RecordCount takes that filter or range into account. However, if the dataset is not a Paradox dataset, the value of RecordCount may be only an approximation when a filter is in effect.

  TCustomADODataSet(RecordCount,RecNo对大部分数据库有效)

  Examine RecordCount to determine the total number of records in the recordset of the dataset component. Applications might use this property with RecNo to iterate through all the records in a dataset, though typically record iteration is handled with calls to First, Last, MoveBy, and Prior using Eof and Bof to set the limits of row traversing.

  The dataset component must be active for RecordCount to provide a valid number. Should ADO not be able to determine that actual number of rows, RecordCount will return a value of negative one (-1).

  ...

  我用bde,可能ado有改进吧

  ...

  不要用recordcount,对桌面型数据库面言,它是有用的,但对大型的它总是返回-1,总之为了兼容性,不要用它,用while not eof,如果要取得记录数,就作个循环吧。

  劝你不要用它了

  ...

  这有可能的,这是Delphi的BUG

  你用ADOQuery试试,我用的都是ADOQuery,没什么问题的。

顶部     1/1 
欢迎评论
未登录,
请先 [ 注册 ] or [ 登录 ]
(一分钟即可完成注册!)
返回首页     ·   返回[Delphi]   ·   返回顶部