Rusmir
Rusmir

Reputation: 43

How to check is connection to database opened?

I have problem: i don't know how to check does connection to database is established.

I used:

Dim sqlConnection As SqlConnection = New SqlConnection(SQLconnectionEntry)
'SQLconnectionEntry is specified earlier
sqlConnection.Open() 

after opening connection, i have to check is it established, but i stuck.

Any suggestions?

Thx.

EDIT:

Also, how to check does connection is NULL? Thx

EDIT:

I found an answer... simply:

Dim isOpen As Boolean If (sqlConnection.State = ConnectionState.Open) Then isOpen = True Else isOpen = False

And it works for me.

Hope that will help for others.

Upvotes: 3

Views: 26827

Answers (2)

Ashish Thakur
Ashish Thakur

Reputation: 323

What about:

if (sqlConnection.State = ConnectionState.Open)Then

//Your Code here

End if

Upvotes: 9

JonH
JonH

Reputation: 33143

You can check the state of your connection like so:

If sqlConnection.State <> ConnectionState.Open Then
End If

Upvotes: 3

Related Questions