try  
 ADOQuery1.ConnectionString:='Provider=SQLOLEDB.1;Password=123456'
                            +';Persist Security Info=True;User ID=sa'
                            +';Initial Catalog=test'
                            +';Data Source=192.168.0.3';
  except
  showmessage('对不起!数据库连接错误!');
  end;我这样写try根本不起作用。如果把IP或用户名给改成错的,程序就会死掉。哪位高手能帮我解决这个问题

解决方案 »

  1.   

    调试会出错
    编译后运行exe文件试试
      

  2.   

    你这句是不会出错的,因为你只为ConnectionString作了赋值只得对ADOQuery1进行active = True或open时才出错
      

  3.   


    try  
      ADOQuery1.ConnectionString:='Provider=SQLOLEDB.1;Password=123456' 
                                +';Persist Security Info=True;User ID=sa' 
                                +';Initial Catalog=test' 
                                +';Data Source=192.168.0.3'; 
      ADOQuery1.SQL := 'select 1';
      ADOQuery1.open;
    except 
      showmessage('对不起!数据库连接错误!'); 
    end; 
      

  4.   

    2楼说得没错,这种写法,如果网络中断,程序会死掉。不在编译环境里,直接运行exe程序一样会死掉。
    我怀疑try  except 为什么不起作用!希望有人教我另外一种方法。
      

  5.   

    直接在程序目录下,执行EXE文件,看是否效果.
      

  6.   

    建议用adoconnection做联接数据库
      

  7.   

    用adoconnection做连接数据库,再用ADOQuery连接adoconnection会不会影响程序效率呢?
    我就直接用ADOQuery心里上感觉应该对程序执行效率会有提高吧
      

  8.   

    分工合作,连接数据库是adoconnection的强项,查询数据是ADOQuery的强项
      

  9.   


    你用adoquery连接也是走的联接串,是一样的,但当你有多个query时还会这样写吗?
    1.增加了联接数
    2.代码重复
      

  10.   

    既然你只做数据链接判断给你个函数吧,很久很久以前写的~ 应该可以用function TestSqlDb(SqlName, DBName, Uid, Pwd, AppName: string;
      ConnType, ConnTime: integer; var RetString: widestring): integer;
    var rel: Byte;
      AdoConn: TADOConnection;
      ConnStr: string;
    begin
      rel := 0;
      try
        begin
          AdoConn := TADOConnection.Create(nil);
          if Conntype = 1 then
            Connstr := 'Provider=SQLOLEDB.1;password=' + Pwd + ';Persist Security Info=true;User ID='
              + Uid + ';Initial Catalog=' + DbName + ';Data Source=' + sqlName + ';application name=' + Appname
          else
            Connstr := 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog='
              + dbname + ';Data Source=' + sqlName + ';application name=' + Appname;
          with AdoConn do
          begin
            ConnectionTimeout := ConnTime;
            CommandTimeout := ConnTime;
            loginprompt := false;
            keepconnection := true;
            connectionstring := Connstr;
            Provider := 'SQLOLEDB.1';
            DefaultDatabase := DBName;
            Connected := true;
            Connected := False;
            Free;
          end;
        end
      except
        on e: exception do
        begin
          Retstring := e.message;
          AdoConn.free;
          rel := 1;
        end;
      end;
      Result := rel;
    end;
      

  11.   

    N个adoquery直接连数据库
    一个adoconnection连接数据库,N个adoquery再与adoconnection相连LZ觉得那个效率更高呢?
      

  12.   

    谢谢大家,我没有想到用多个doquery的问题,感谢提醒。呵呵!