我有一个控制服务器连接的功能。如果 ado 无法在 5 秒内连接,它应该会报错。但是 connectiontimeout 属性不能工作。
这是我正在使用的代码:
function AdoConnectionTester(strServerName, strUserName, strPassword,
strDBName: string; boolShowMessage: boolean): Boolean;
var
ADOConn: TADOConnection;
begin
try
Result := True;
ADOConn := TADOConnection.Create(nil);
ADOConn.LoginPrompt :=False;
ADOConn.Close;
ADOConn.ConnectionString := 'Provider=SQLOLEDB.1; Password='+strPassword+';'+
'Persist Security Info=True;User ID='+strUserName+';'+
'Initial Catalog='+strDBName+';'+
'Data Source='+strServerName;
try
ADOConn.ConnectionTimeout := 5;
ADOConn.Open;
except
on E: Exception do
begin
Result := False;
ShowMessage(E.Message);
end;
end;
if Result then
if boolShowMessage = True then
ShowMessage('OK');
finally
ADOConn.Free;
end;
end;
我怎么解决这个问题 ?