con:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=;Data Source=';//无数据库
    Adoconnection1.ConnectionString:='';
    Adoconnection1.ConnectionString:=con;
    showmessage(con);  try   
      adoconnection1.connected := true;   
      showmessage(连接成功);   
  except     
      showmessage(连接失败);   
  end;为什么总是显示‘连接成功’,用执行文件也如此。
到底如何判断有无ADO连上呢?

解决方案 »

  1.   

    不可能显示"连接成功",应该显示"连接失败"
    你加一名:Adoconnection1.connected := false;试试.
    如果你要判断是否与数据库连接正常,你可以加一个查询系统表的查询,这样就能更准备的得到与数据库是否正常了.
      

  2.   

        con:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=;Data Source=';//无数据库 
        Adoconnection1.ConnectionString:=''; 
        Adoconnection1.ConnectionString:=con; 
        showmessage(con);   try    
          adoconnection1.connected := False;    
          showmessage(连接成功);    
      except      
          showmessage(连接失败);    
      end; 结果还是‘连接成功’!!!
      

  3.   

    用个IF THEN 判断不就好了嘛,
      

  4.   

    adoconnection1.connected := true; 
    这一行代码可以完成对你指定的数据库的连接
    adoconnection1.connected := False; 
    断开连接。只要你的数据库存在,或者没有什么系统内部的意外,都会成功执行,至少不会raise 错误,所以你的  except  部分根本就不会执行。adoconnection1.connected := true; 
    if adoconnection1.connected then
      showmessage(连接成功)
    else
      showmessage(连接失败); 以上代码可能管用 
      

  5.   

    代码本身就是错误的。
    showmessage(连接成功) =》showmessage('连接成功') 
    连接成功是正确的, 连接成功代表你和SQLServer取得了通讯。
    你可以动态获取你要的数据库
      

  6.   

    只要这句adoconnection1.connected := true; 执行完后,程序不抛出异常,就已经连接上数据库了;
    接下来你就可以进行相关的数据库操作;
    //-------------------------------
    另外二楼的说把adoconnection1.connected := true; 中的true改成false,不知道你的本意是什么?
    改成false后继续执行下去,当然也是'连接成功',因为已经连接上数据库了,程序会继续执行;
      

  7.   

        con:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=;Data Source=';//无数据库   
        Adoconnection1.ConnectionString:=con;  
        showmessage(con);  
        adoconnection1.connected := False; 
      try     
          adoconnection1.connected := true; 
          //这里可以加一句查询系统表的语句
           with adoqueyr1 do
          begin 
            close;        
            sql.text := ' select getdate ';//sqlserver  oracle这样写 select from dual';
            open;   //你看现在抛不抛异常         
          end;   
          showmessage('连接成功');     
      except       
          showmessage('连接失败');     
      end;  
      

  8.   

    既然显示连接成功,那就说明连接上了呗。你试试关掉SQL SERVER服务看还显不显示连接成功。
      

  9.   

      try     
          adoconnection1.connected := False;     
          showmessage(连接成功);     
      except       
          showmessage(连接失败);     
      end;  
    我晕,这样写,永远会显示连接成功,错误有其二.一adoconnection1.connected := False;  改为adoconnection1.connected := True;  
    二、showmessage(连接成功);  怎么字符串,‘’都没有了。   
      

  10.   

    4楼说的对。先设置adoconnection1.connected:=true了,必然显示连接成功。
    如果你这么写,就是连接失败的。
    if adoconnection1.connected then
          showmessage('连接成功')
         else
          showmessage('连接失败');
      

  11.   


    con:='Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=;Data Source=';//无数据库
        Adoconnection1.ConnectionString:='';
        Adoconnection1.ConnectionString:=con;
        showmessage(con);  try   
          adoconnection1.Connect;
          if adoconnection1.Connected then
            showmessage(连接成功)  
      except     
          showmessage(连接失败);   
      end; nnd,偶最讨厌代码不注意大小写影响市容。