客户端用ado连接 没有开机的sql服务器,会卡住10秒以上,然后提示连接失败。为了解决这个问题,我在连接前放了个显示gif等待动画的form,问题是这样显示出来的form也是一样会卡死。如果用线程,线程执行到一些界面上的语句后会自动停止。请问应该如何处理呢?或者哪位有代码吗?万分谢谢

解决方案 »

  1.   

    放到线程中,timer不行,timer不是线程,会阻塞,线程访问主程序要同步
    另外你可以显示一个等待窗体,Show后,加上Application.PostMessage,让窗体显示出来
      

  2.   

    ADOConnection有个ConnectionTimeout属性,就是连接超时的设置,
    根据需要,你设置一下就可以了!
      

  3.   

    无论你怎么设置它,那十多秒的时间,是没法逃脱的。我放了个“快速检测sql连接”demo在我加入的“中国软件研发联盟Q群”的共享里,请参考。
      

  4.   


    简单的写写
    type
      DBConnect = class(TThread)
      private
        { Private declarations }
      Fadocon :TADOConnection;
      protected
        procedure Execute; override;
      public
        constructor create(createsuspend:Boolean; adocon :TADOConnection);
        destructor destroy;override;
      end;implementationconstructor DBConnect.create(createsuspend:Boolean; adocon :TADOConnection);
    begin
      FreeOnTerminate := True;
      Fadocon := adocon;
      adocon := TADOConnection.Create(nil);
      inherited create(True);
    end;destructor DBConnect.destroy;
    begin
      inherited;
    end;
    procedure DBConnect.Execute;
    var
      connsql:string;
    begin
      CoInitialize(nil);
      connsql := 'Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=StockDB;Data Source=192.164.12.23';
      Fadocon.CommandTimeout := 30;
      Fadocon.ConnectionString := connsql;
      try
        Fadocon.Connected;
        Fadocon.Open;
      except
      end;
      CoUninitialize;
      { Place thread code here }
    end;窗体调用:
    procedure TForm1.Button1Click(Sender: TObject);
    var
        th1 :DBConnect;
    begin  th1 := DBConnect.create(True,ADOConnection1);
      th1.Resume;
    end;
      

  5.   


    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, DB, ADODB,activex;type
      TMyThread = class(TThread)
      protected
        FADO: TADOConnection;
        FFormHandle: Longword;
        procedure Execute; override;
      public
        constructor Create(AFormHandle: Longword; AADOConnection: TADOConnection); reintroduce;  end;
      TForm1 = class(TForm)
        ADOConnection1: TADOConnection;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        procedure DoConnectMsg(var Message: TMessage); message wm_user+1;
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementationuses ComObj;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
     //在子线程里打开数据库连接
     TMyThread.Create(self.Handle,ADOConnection1);
    end;{ TMyThread }constructor TMyThread.Create(AFormHandle: Longword; AADOConnection: TADOConnection);
    begin
      inherited Create(True);
      FreeOnTerminate := True;
      FADO := AADOConnection;
      FFormHandle := AFormHandle;
      Resume;
    end;procedure TMyThread.Execute;
    begin
      CoInitialize(nil);  try
        FADO.Connected := true;
      except
          ;
      end;
      PostMessage(FFormHandle,wm_user+1,0,0); //给主程序发连接完成通知  CoUninitialize;
    end;procedure TForm1.DoConnectMsg(var Message: TMessage);
    begin
      if ADOConnection1.Connected then
        ShowMessage('连接成功了')
      else
        ShowMessage('连接失败');end;end.
      

  6.   

    zzflover的代码很好,但是有一个问题我研究了也没能解决,就是第一次连接肯定会失败,之后都是成功的,请问这是什么原因呢?
      

  7.   

    第一次执行单步执行到:end就不再往下了。只能按F9执行到一次执行完后面的语句
    destructor DBConnect.destroy;
    begin
      inherited;
    end;
      

  8.   

    我用的语句如下:constructor DBConnect.create(createsuspend:Boolean; adocon :TADOConnection);
    begin
      FreeOnTerminate := True;
      Fadocon := adocon;
      adocon := TADOConnection.Create(nil);
      inherited create(True);
    end;destructor DBConnect.destroy;
    begin
      inherited;
    end;
    procedure DBConnect.Execute;
    var
      connsql:string;
    begin
      CoInitialize(nil);
      Fadocon.CommandTimeout := 30;
      Fadocon.ConnectionString := 'Provider=SQLOLEDB.1;PassWord=mydate;Persist Security Info=True;User ID=Jstzgs;Initial Catalog=''mdata'';Data Source=124.432.136.28';  Fadocon.LoginPrompt:=False; try
        Fadocon.Connected;
        Fadocon.Open;
        ifcon:=True;
      except
        ifcon:=false;
      end;
      CoUninitialize;end;
    procedure TFormLogin.Button1Click(Sender: TObject);
    var
        th1 :DBConnect;
    begin
           FConWait:=TFConWait.Create(Application);
           FConWait.Show;       th1 := DBConnect.create(True,ADOConnection1);
           th1.Resume;            if false=ifcon then
              begin
                 ShowMessage('连接服务器失败!');
               Exit;
              end else  ShowMessage('连接服务器成功!');
               FConWait.Close;end;
      

  9.   

    那是因为你的 ifcon ,你好好想想
      

  10.   

    是不是不能用变量?可是不用变量怎么知道在bitbtn事件中知道是否连接成功,然后操作下在的语句呢?请高手指点,我调试了很久都没弄出来。