客户端用ado连接 没有开机的sql服务器,会卡住10秒以上,然后提示连接失败。为了解决这个问题,我在连接前放了个显示gif等待动画的form,问题是这样显示出来的form也是一样会卡死。如果用线程,线程执行到一些界面上的语句后会自动停止。请问应该如何处理呢?或者哪位有代码吗?万分谢谢
解决方案 »
- 急求 如下代码如何修改可以锐化jpg图片
- 想念以前的斑竹,特想念aiirii,aiirii特热情,有求必应!但是现在的斑竹...在类似非技术区见他们的身影比较多!
- error $2108是什么意思?如何解决?
- 求解数学题??? 假设有2000万美圆 = 1.6亿人民币,每建设一个希望学校要花20万人民币,请可以建设多少个学校???
- 凑个热闹!
- oracle!Oracle!oracle!Oracle!oracle!Oracle!
- 用lable组件的时候如果输入中文经常会出现字体变形或重叠的情况怎么解决啊
- 我用delphi写了一个千千动听,有些地方不大了解,谁能帮我完善它?大神,快来吧!!
- "abstract error"到底是什么错误?急盼各位大虾帮助!
- 有没有好一点的关于Delphi网络编程的书!!!
- delphi 7 使用问题
- 有用过dxdbtreelist的吗?设置一列为选择列(checkcolum)的时候,选择怎么用代码控制?
另外你可以显示一个等待窗体,Show后,加上Application.PostMessage,让窗体显示出来
根据需要,你设置一下就可以了!
简单的写写
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;
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.
destructor DBConnect.destroy;
begin
inherited;
end;
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;