如何检查服务器是否打开并显示一个消息框

时间:2015-12-03 16:56:17

标签: mysql vb6

HHI !!我想显示一个消息框,如果服务器没有启动这是class1的代码,在此代码中当xampp和mysql服务器未打开时,它们会显示错误,所以我希望他们显示消息框' not再次连接tyr' !!。

这是class1的代码

Private Sub Class_Initialize()
DoEvents
Set con = New ADODB.Connection
With con

.ConnectionString = "Driver={MYSQL ODBC 5.2 ANSI Driver} ;server=localhost;pwd=;uid=root;port=3306;database=kibaza;"
.CursorLocation = adUseClient
.Open
End With
End Sub

3 个答案:

答案 0 :(得分:3)

为您的代码添加错误处理程序。

Private Sub Class_Initialize()
    DoEvents

    On Error GoTo err_Class_Initialize

    Set con = New ADODB.Connection
    With con
        .ConnectionString = "Driver={MYSQL ODBC 5.2 ANSI Driver} ;server=localhost;pwd=;uid=root;port=3306;database=kibaza;"
        .CursorLocation = adUseClient
        .Open
    End With

    Exit Sub

err_Class_Initialize:
    'You can just display the error to the user, or you can examine the err number and make decisions
    MsgBox "There was an error opening the connection." & vbCrLf & vbCrLf & _
           "Error: " & CStr(Err.Number) & ", " & Err.Description, vbOKOnly + vbExclamation, "Initialization Error"

End Sub

答案 1 :(得分:0)

使用“jac”的概念,以这种方式放置“err.number<> 0”只有在出现错误时才会激活“GoTo”:

Private Sub Class_Initialize()
DoEvents

On Error GoTo err_Class_Initialize

Set con = New ADODB.Connection
With con
    .ConnectionString = "Driver={MYSQL ODBC 5.2 ANSI Driver} ;server=localhost;pwd=;uid=root;port=3306;database=kibaza;"
    .CursorLocation = adUseClient
    .Open
End With

err_Class_Initialize:
if err.number <> 0 then
    'You can just display the error to the user, or you can examine the err number and make decisions
    MsgBox "There was an error opening the connection." & vbCrLf & vbCrLf & _
        "Error: " & CStr(Err.Number) & ", " & Err.Description, vbOKOnly + vbExclamation, "Initialization Error"
end if
End Sub

答案 2 :(得分:0)

我建议完全重写。

请勿在Class_Initialize事件期间尝试建立连接。而是添加某种Connect方法。

声明连接WithEvents并异步打开。为Connection对象的ConnectComplete事件实现处理程序。在那里,您可以处理adStatusError对象以确定成功或失败,并将事件发回给您的班级客户,以指明状态。

您似乎试图以可以想象的最原始的方式使用ADO。请注意您在网络上找到的代码片段,其中大部分源于20世纪90年代的ASP VBScript样本。