VB.NET函数返回错误并退出子

时间:2017-04-07 09:03:20

标签: mysql vb.net mysqlconnection

我的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

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

如何通过给出connection not available at the moment, please try again later之类的msgbox然后退出使用该函数的子来避免异常?

End Sub

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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 requires an open and available Connection. The connection's current state is Connecting

相关问题