2014-01-24 3 views
0

mysqlconnection으로 연결이 이미 열려 있는지 어떻게 확인할 수 있습니까?연결을 열기 전에 SqlConnection 상태를 어떻게 확인합니까?

Connection = New MySqlConnection() 
Connection.ConnectionString = ConfigurationManager.ConnectionStrings("DbConncetionString").ConnectionString 
Connection.Open() 
SQL = "SELECT * from TBL_PARKERS where id=" & encode(Request.Cookies("parker").Values("id")) & "" 
Dim Query As MySqlCommand = New MySqlCommand(SQL, Connection) 
'Here i want to check connection is open or close 
Connection.Open() 
+0

두 번 열면 어떤 문제가 발생할 수 있습니까? "아니오"일 경우 귀찮게하지 마십시오. –

답변

3

MySqlConnectionDbConnection에서 파생 감안할 때 당신이 State 속성을 예를 들어, 확인하실 수 있습니다 : 나는 연결이 이미 열려있는 경우 다음 다시 열 필요가 없습니다 것을 원하는

Connection.State == ConnectionState.Open 
0

이 기능을 테스트 연결에 사용할 수 있습니다.

공공 기능 TestConn (문자열로 ByVal의 서버)

Using tcpSocket As New System.Net.Sockets.TcpClient 
     Try 
      Dim async As IAsyncResult = tcpSocket.BeginConnect(server, 1433, Nothing, Nothing) 
      Dim startTime As DateTime = DateTime.Now 

      Do 
       System.Threading.Thread.Sleep(500) 
       If (async.IsCompleted) Then 
        Exit Do 
       End If 
      Loop While (DateTime.Now.Subtract(startTime).TotalSeconds < 5) 

      If (async.IsCompleted) Then 
       tcpSocket.EndConnect(async) 
       Return True 
      End If 
      tcpSocket.Close() 

      If (async.IsCompleted = False) Then 
       Return False 
      End If 

     Catch ex As Exception 

      My.Application.Log.WriteEntry("[" & DateTime.Now & "] - " & ex.Source & ": " & ex.Message) 


     End Try 
    End Using 
End Function 

부울

으로하는 개방 TCP/IP 포트 번호 1433

안부를 기억하십시오!