在编程时遇到一个奇怪的事情  代码如下:  
 
function  ExistRecord(age:integer):boolean;;  
var  AQ:Tadoquery;  
begin  
   result:=false;  
   if  not  main.ADOConnection.Connected  then  exit;  
   AQ:=Tadoquery.Create(nil);          
   try  
         AQ.Connection:=main.ADOConnection;  
         AQ.Sql.text:='select  *  from  mydatabase.dbo.mytable  where  age='+inttostr(age);  
         AQ.Open;
         if  AQ.RecordCount>0  then  result:=true;  
   finally    
         AQ.Free;  
   end;                          
end;  
 
可是,明明存在的记录偶尔就查不到,本函数是在一个线程中被执行的。  
请高手指教,是不是  Delphi的错,或是  SqlServer  的错,或是代码的用法不对,  
如果代码的用法不对,请具体解释。谢谢!

解决方案 »

  1.   

    function  ExistRecord(age:integer):boolean;;  
    var  AQ:Tadoquery;  
    begin  
       result:=false;  
       if  not  main.ADOConnection.Connected  then  exit;  
       AQ:=Tadoquery.Create(nil);          
       try  
             AQ.Connection:=main.ADOConnection;  
             AQ.close;
             AQ.sql.clear;   
             AQ.Sql.text:='select  *  from  mydatabase.dbo.mytable  where  age='+inttostr(age);  
             AQ.Open;
             if  AQ.RecordCount>0  then  result:=true;  
       finally    
             AQ.Free;  
       end;                          
    end;
      

  2.   

    是不是别的地方有锁?或者synchronize?
      

  3.   

    to  ln521(*逃课小王子*)  不用close了,刚刚create的
      

  4.   

    动态创建的 Tadoquery不存在“别的线程把表锁了”且 Tadoquery 自动维护自身的SESSION
    那么,到底问题出在那里呢?是 出自 sqlserver 吗,还是 Delphi 的 BUG.
      

  5.   

    你能确定 RecordCount 的值是正确的吗?会不会每一次都是-1呢?       if  AQ.RecordCount>0  then  result:=true;  
    将这句尝试修改为
     result := not AQ.Eof;
    试试。
      

  6.   

    首先还是不要认为是工具的BUG,你的线程FreeOnTerminate设为True了没?我感觉好像是线程的冲突问题
      

  7.   

    AQ.RecordCount 不是返回的记录条数吗?
    线程FreeOnTerminate当然设为True了。
    没有人遇到过此类问题吗?
      

  8.   

    recordcount 有时计算出错,不推荐使用,这是delphi,ado bug,建议用 
    result := not AQ.Eof;代替
      

  9.   


       AQ:=Tadoquery.Create(nil);          
       try  
             AQ.Connection:=main.ADOConnection;  
             AQ.close;
             AQ.sql.clear;   
             AQ.Sql.text:='select  *  from  mydatabase.dbo.mytable  where  age='+inttostr(age);  
             AQ.Open;
             if  AQ.RecordCount>0  then  result:=true;  
       finally    
             AQ.Free;  
       end;                          
       AQ:=Tadoquery.Create(nil);          
       try  
             AQ.Connection:=main.ADOConnection;  
             AQ.close;
             AQ.sql.clear;   
             AQ.Sql.text:='select  count(*)  from  mydatabase.dbo.mytable  where  age='+inttostr(age);  
             AQ.Open;
             AQ.First;
             if  AQ.Fields[0].AsInteger>0  then  result:=true;  
       finally    
             AQ.Free;  
       end;
      

  10.   

    改成AQ.Sql.text:='select  *  from  mydatabase.dbo.mytable  where  age='+QuotedStr(inttostr(age)); 
    试试
      

  11.   

    adoquery.recordcount 有时的确会出错,有时明明显示出了数据,但recordcount的值是-1,但有时又是好的,所以建议你用CloneCenter(复制中心)的方法试试。我以前要查询recordcount都是用select count(*) from ...来查查的
      

  12.   

    DB单元中DataSet的属性RecordCount的取值是通过如下虚(virtual)函数:
    function TDataSet.GetRecordCount: Longint;
    begin
      Result := -1;
    end;ADODB单元中ADOCustomDataSet重写(override)了该函数;如下:
    function TCustomADODataSet.GetRecordCount: Longint;
    begin
      CheckActive;
      Result := Recordset.RecordCount;
    end;
    //:CheckActive是判断数据集是否打开。Recordset.RecordCount他直接调用原生的ADO对象的函数Get_RecordCount: Integer;safecall;
    因为TADOQuery是从TCustomADODataSet继承而来。所以,这就应该跟Delphi没有关系了,可能是ADO的哪个参数的设置问题或是Bug。