【问题标题】:VB.NET function return error and exit subVB.NET函数返回错误并退出子
【发布时间】:2023-03-21 14:15:02
【问题描述】:

我的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 的消息框来避免异常,然后退出正在使用该函数的子程序?

结束子

【问题讨论】:

  • 这是 OpenMysqlCon,我已经更正了错字,对不起朋友。

标签: mysql vb.net mysqlconnection


【解决方案1】:

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

所以不要使用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

【讨论】:

  • Return Nothing 还是一样,因为我还是会有例外。
  • @JosephGoh:然后返回任何你想要的东西。那不是重点。关键是您不应该在使用它的方法之外保持连接。当然,您必须处理该方法返回Nothing 的情况,无论您将调用此方法。
【解决方案2】:

会是这样的。

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

【讨论】:

  • 我正在尝试使用using 语句,所以我不必费心在代码末尾关闭连接。
猜你喜欢
  • 1970-01-01
  • 2016-05-13
  • 2016-06-11
  • 1970-01-01
  • 2018-12-19
  • 2018-02-17
  • 2017-10-14
  • 2018-07-10
  • 1970-01-01
相关资源
最近更新 更多