0

我的 VB.NET 项目中有这个连接功能

Public Function OpenMysqlCon() As MySqlConnection
    Dim mMysqlconnection = New MySqlConnection()
    Try
        Dim strconDB As String = "server='192.168.100.2'; database='mydb';Port=3306; UID='epb'; password='hahaha'; pooling=true"
        mMysqlconnection = New MySqlConnection
        mMysqlconnection.ConnectionString = strconDB
        mMysqlconnection.Open()
        OpenMysqlCon = mMysqlconnection
    Catch exceptionThatICaught As System.Exception
        OpenMysqlCon = Nothing
    End Try
End Function

我将在我的 VB 项目中调用该函数,例如

Private Sub frmTest_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Using Con=OpenMysqlCon()
        'mycode here
    End Using
End Sub

但是,当连接不可用时,它会抛出异常。

我怎样才能通过给一个 msgbox 类似的东西connection not available at the moment, please try again later然后退出正在使用该函数的子程序来避免异常?

结束子

4

2 回答 2

0

它会是这样的。

Public Function OpenMysqlCon() As MySqlConnection
    Dim mMysqlconnection As MySqlConnection()
    Try
        Dim strconDB As String = "server='192.168.100.2'; database='mydb';Port=3306; UID='epb'; password='hahaha'; pooling=true"
        mMysqlconnection = New MySqlConnection
        mMysqlconnection.ConnectionString = strconDB
        mMysqlconnection.Open()
    Catch exceptionThatICaught As System.Exception
        mMysqlconnection = Nothing
    End Try
    Return mMysqlconnection
End Function

Private Sub frmTest_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim con As MySqlConnection = OpenMysqlCon
    If con Is Nothing Then
        MessageBox.Show("connection not available at the moment, please try again later")
        'Me.Close 'uncomment if the app should end
    End If
End Sub

编辑 - 这未经测试

    Using con As MySqlConnection = OpenMysqlCon
        If con Is Nothing Then
            MessageBox.Show("connection not available at the moment, please try again later")
            'Me.Close 'uncomment if the app should end
        Else
            'your code
        End If
    End Using
于 2017-04-07T09:18:40.373 回答
0

最好的方法是不在表单的字段中保持连接,而是在需要的地方创建和初始化它。连接池将确保不需要创建物理连接。

所以不要使用任何OpenMysqlCon方法,而是这样的代码(GetAllUsers只是一个例子):

Public Function GetAllUsers() As List(Of User)
    Dim userList = New List(Of User)

    Try
        Using mMysqlconnection = New MySqlConnection("server='192.168.100.2'; database='mydb';Port=3306; UID='epb'; password='hahaha'; pooling=true")
            mMysqlconnection.Open()
            Using command = New MySqlCommand("SELECT * FROM USER ORDER By UserName", mMysqlconnection)
                Using rdr = command.ExecuteReader()
                    While rdr.Read()
                        Dim user = New User()
                        user.UserName = rdr.GetString(0)
                        userList.Add(user)
                    End While
                End Using
            End Using
        End Using
    Catch exceptionThatICaught As System.Exception
        MessageBox.Show("meaningful message here, logging would be useful too")
        Return Nothing
    End Try

    Return userList
End Function

可能有帮助,因为相关(您也在重用连接):ExecuteReader 需要一个开放且可用的连接。连接的当前状态是 Connecting

于 2017-04-07T09:29:47.003 回答