vb.net 如何检查与数据库的连接是否打开?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34863111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check is connection to database opened?
提问by Rusmir
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
另外,如何检查连接是否为NULL?谢谢
EDIT:
编辑:
I found an answer... simply:
我找到了一个答案......简单地说:
Dim isOpen As Boolean If (sqlConnection.State = ConnectionState.Open) Then isOpen = True Else isOpen = False
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.
希望这会对其他人有所帮助。
回答by Ashish Thakur
What about:
关于什么:
if (sqlConnection.State = ConnectionState.Open)Then
//Your Code here
End if
回答by JonH
You can check the state of your connection like so:
您可以像这样检查连接状态:
If sqlConnection.State <> ConnectionState.Open Then
End If

