2017-04-07 4 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のようなものを与え、その関数を使用していたサブを終了すると、例外を回避できますか?

エンドサブ

+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'を返すケースを処理する必要があります。 –

関連する問題