如果服务器不存在,如何使 ADO 连接超时更快?
我的任务是支持一个旧的 VB6 应用程序。 (是的)我在 ADO 连接超时属性方面遇到了问题。如果服务器存在,下面的方法可以正常工作,但是如果服务器不存在或机器的网络连接尚未启动,即使将 intTimeout 设置为 1,也将花费整整 30 秒的时间来超时。ADO
有没有办法无法更快连接? 这可能吗? 谢谢!
Public Sub GetConnectionObject(ByRef oCn As ADODB.Connection, strServer As String, strInitialCatalog As String, Optional intTimeout = 10)
Dim strConnectionString As String
strConnectionString = "Data Source=[SERVER];Provider=SQLOLEDB.1;User ID=ScanReq1;Password=ScanR3Q;Initial Catalog=[INITIALCATALOG];ConnectionTimeout=" & intTimeout & ";"
strConnectionString = Replace(strConnectionString, "[SERVER]", strServer)
strConnectionString = Replace(strConnectionString, "[INITIALCATALOG]", strInitialCatalog)
Set oCn = New ADODB.Connection
oCn.CursorLocation = adUseClient
oCn.ConnectionString = strConnectionString
oCn.CommandTimeout = intTimeout
oCn.ConnectionTimeout = intTimeout
oCn.Open
End Sub
I have been tasked w/ supporting an old VB6 app. (yay me) I am having trouble with the ADO connection timeout property. The method below works fine if the server exists, but if the server does not exist or network connections havent started up for the machine it will take a full 30 seconds to timeout even with the intTimeout set to 1.
Is there a way for ADO to fail to connect sooner?
Is this even possible?
Thanks!
Public Sub GetConnectionObject(ByRef oCn As ADODB.Connection, strServer As String, strInitialCatalog As String, Optional intTimeout = 10)
Dim strConnectionString As String
strConnectionString = "Data Source=[SERVER];Provider=SQLOLEDB.1;User ID=ScanReq1;Password=ScanR3Q;Initial Catalog=[INITIALCATALOG];ConnectionTimeout=" & intTimeout & ";"
strConnectionString = Replace(strConnectionString, "[SERVER]", strServer)
strConnectionString = Replace(strConnectionString, "[INITIALCATALOG]", strInitialCatalog)
Set oCn = New ADODB.Connection
oCn.CursorLocation = adUseClient
oCn.ConnectionString = strConnectionString
oCn.CommandTimeout = intTimeout
oCn.ConnectionTimeout = intTimeout
oCn.Open
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TCP 连接建立后,ConnectionTimeout 开始计时。如果找不到服务器,则该值由 Windows TCP 子系统控制。
如果这对您来说确实是个问题,我会尝试先对盒子进行 ping 操作(网上有很多通过 VB6 进行 ping 操作的示例)。
The ConnectionTimeout kicks in after the TCP connection is made. If the server can't be found, this value is controlled by the Windows TCP subsystem.
If this really is an issue for you, I'd try to ping the box first (there are plenty examples of pinging via VB6 on the net).
我也中过这个。设置
ConnectionTimeout
的另一种方法是使Open
调用异步,然后在您自己的代码中处理超时。下面是快速而肮脏的示例(注意:这是用 VBA 编写的,但应该可以轻松移植到 VB6):为了“正确”执行此操作,需要使用一个
ConnectionComplete
事件可以处理。I've also hit this one. An alternative to setting
ConnectionTimeout
could be to make theOpen
call asynchronous, then handle the timeout in your own code. Quick and dirty example below (note: this is in VBA, but should be easily ported to VB6):To do it "properly", there is a
ConnectionComplete
event which you could handle.