2017-04-07 1 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 

에이 연결 기능을 가지고 있고 그러나

Private Sub frmTest_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Using Con=OpenMysqlCon() 
     'mycode here 
    End Using 
End Sub 

처럼 내 VB 프로젝트 뭔가에있는 함수를 호출합니다 연결을 사용할 수 없으면 예외가 발생합니다.

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하는 것입니다 테스트되지 않은 상태입니다. 연결 풀링은 물리적 연결을 만들 필요가 없음을 보장합니다.

그래서 (GetAllUsers 그냥 예입니다)이 같은 모든하지만 코드에서 OpenMysqlCon 방법을 사용하지 마십시오

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'을 리턴하는 경우를 처리해야합니다. –