Mysql VB.NET函数返回错误并退出子对象
我在VB.NET项目中有这个连接函数Mysql VB.NET函数返回错误并退出子对象,mysql,vb.net,mysqlconnection,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=t
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提供像连接这样的连接来避免异常,此时不可用,请稍后重试
,然后退出正在使用该功能的子系统
End Sub它将是这样的
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
最好的方法不是将连接保存在表单的字段中,而是在需要的地方创建并初始化它。连接池将确保不需要创建物理连接 因此,不要使用
OpenMysqlCon
方法,而是使用这样的代码(GetAllUsers
只是一个示例):
可能有帮助,因为相关(您也在重复使用连接):这是OpenMysqlCon,我已经更正了输入错误,抱歉,朋友。我正在尝试使用
使用语句,所以我不必费心在代码结束时关闭连接。当不返回任何内容时,
它仍然是一样的,因为我还是会有例外。@JosephGoh:那你想要什么就给我什么。这不是重点。关键是,您不应该将连接保持在使用它的方法之外。当然,无论在哪里调用该方法,都必须处理该方法返回Nothing
的情况。
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