如果服务器不存在,如何使 ADO 连接超时更快?

发布于 2024-11-28 10:16:21 字数 949 浏览 1 评论 0原文

我的任务是支持一个旧的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不吻晚风 2024-12-05 10:16:21

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).

白日梦 2024-12-05 10:16:21

我也中过这个。设置 ConnectionTimeout 的另一种方法是使 Open 调用异步,然后在您自己的代码中处理超时。下面是快速而肮脏的示例(注意:这是用 VBA 编写的,但应该可以轻松移植到 VB6):

Dim conn As New ADODB.Connection
Dim time As Single, timeOut As Single
conn.ConnectionString = "your connection string here"
conn.Open Options:=adAsyncConnect  ' value is 16
timeOut = 5
time = Timer()
Do Until Timer() - time > timeOut Or conn.State = adStateOpen
    DoEvents
Loop
If conn.State <> adStateOpen Then    'value is 1
    'timed out
Else
    'successful
End If

为了“正确”执行此操作,需要使用一个 ConnectionComplete 事件可以处理。

I've also hit this one. An alternative to setting ConnectionTimeout could be to make the Open 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):

Dim conn As New ADODB.Connection
Dim time As Single, timeOut As Single
conn.ConnectionString = "your connection string here"
conn.Open Options:=adAsyncConnect  ' value is 16
timeOut = 5
time = Timer()
Do Until Timer() - time > timeOut Or conn.State = adStateOpen
    DoEvents
Loop
If conn.State <> adStateOpen Then    'value is 1
    'timed out
Else
    'successful
End If

To do it "properly", there is a ConnectionComplete event which you could handle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文