数据库结构如下:CREATE TABLE [card] (
[card_id] [nvarchar] (12) NOT NULL ,
[card_account] [nvarchar] (10) NOT NULL ,
[password] [nvarchar] (8) NOT NULL ,
[card_type] [int] NOT NULL ,
[card_amount] [int] NOT NULL ,
[exp_time] [nvarchar] (10) NOT NULL ,
[is_used] [tinyint] NOT NULL ,
[is_valid] [tinyint] NOT NULL ,
[used_time] [nvarchar] (20) NULL ,
[create_date] [nvarchar] (10) NOT NULL 

程序代码如下:  try
        with userdata.cardadoqry do
        begin
           close;
           sql.clear;
           sql.Add('select max(card_account) from card');
           open;
        end;
        if userdata.cardadoqry.RecordCount<>0 then
        begin
           maxaccount:=userdata.cardadoqry.fields[0].asstring;
           numaccount:=strtoint64def(maxaccount,0);
           if numaccount<9999999999 then
             numaccount:=numaccount+1
           else numaccount:=1000000000;
           edt_firstaccount.Text:=inttostr(numaccount);
        end
        else edt_firstaccount.Text:='1000000000';    except
       MessageDlg('数据库连接失败,请重试!',mtinformation,[mbok],0);
    end;
奇怪的就是,如果数据库里面一条信息都没有的话,跟踪调试发现它也不会执行
userdata.cardadoqry.RecordCount=0 的操作,而是不管怎么样,都显示Recordcount最少为1,想不通,第一次遇到! (用C/S机构的)

解决方案 »

  1.   

    这和使用Max有关,'select max(card_account) from card' 无论如何都会返回一条记录的,就算数据库没有记录也会还回一个表达式,在数据库中执行该语句你就会发现。
      

  2.   

    用IsEmpty 不要用recordCount好像recordCount 总会不时的出现一些莫名其妙的错,如果你移动一下记录也许就没有这样的错误了
      

  3.   

    userdata.cardadoqry 的sql语句如下select max(card_account) from card 所以肯定有一条记录呀,当表中没有数据时,select max(card_account) from card 得到的记录为null而已,而不是没有记录呀,所以Recordcount=0的条件永远不成立呀!
      

  4.   

    用select max(fieldName) from sourcetable的时候如果数据为空的时候返回的是一个null值,但是记录数recordCount不是为0 而是为1,你可以如下:
       if not fields[0].isNull then 
       begin
         maxaccount:=userdata.cardadoqry.fields[0].asstring;
               numaccount:=strtoint64def(maxaccount,0);
               if numaccount<9999999999 then
                 numaccount:=numaccount+1
               else numaccount:=1000000000;
               edt_firstaccount.Text:=inttostr(numaccount);
      end
      else edt_firstaccount.Text:='1000000000';