2017-04-07 34 views
0

我有這方面的功能在我的VB.NET工程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的東西來避免異常,然後退出使用該函數的子?

End Sub

+0

它是OpenMysqlCon,我修正了錯字,對不起朋友。 –

回答

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 
+0

我正在嘗試使用'using'語句,所以我不必費心去關閉代碼結尾處的連接。 –

+0

@JosephGoh - 見編輯。 – dbasnett

0

最好的辦法是不要持有形式的現場連接,但創造何地您需要它initalize它。連接池將確保不需要創建物理連接。

所以不要在這樣所有,但代碼中使用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

+0

當'Return Nothing'時,它仍然是一樣的,因爲我仍然會有例外。 –

+0

@JosephGoh:然後返回你想要的東西。那不是重點。關鍵是你不應該把連接放在你使用它的方法之外。當然你必須處理這個方法返回'Nothing'的情況,無論你在哪裏調用這個方法。 –