Checking db connection open status

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I am creating a db connection using the below code. Is there a way to check
if the connection is open?

Thanks

Regards


= Code Below ====================

Dim LocalConn As OleDb.OleDbConnection
Dim St As String = My.Settings.LocalDBConnectionString

LocalConn = New OleDb.OleDbConnection(St)
LocalConn.Open()
 
Hi

I am creating a db connection using the below code. Is there a way to check
if the connection is open?

Thanks

Regards

= Code Below ====================

Dim LocalConn As OleDb.OleDbConnection
Dim St As String = My.Settings.LocalDBConnectionString

LocalConn = New OleDb.OleDbConnection(St)
LocalConn.Open()

Check the connection's State property.

Thanks,

Seth Rowe
 
Ah, checking the State might not help. It merely indicates the state that
ADO.NET "thinks" the connection is in--Open or Closed (or some others).
While this might be enough, you won't know if the connection is viable until
you try to use it.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
 
So if it is closed for anyone reason it can be re-opened before executing a
query. Sort of trying to avoid the above mentioned error.

Regards

Miha Markic said:
Why do you need to check its status anyway?

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

John said:
Hi

I am creating a db connection using the below code. Is there a way to
check if the connection is open?

Thanks

Regards


= Code Below ====================

Dim LocalConn As OleDb.OleDbConnection
Dim St As String = My.Settings.LocalDBConnectionString

LocalConn = New OleDb.OleDbConnection(St)
LocalConn.Open()
 
I think the best way to deal with connection errors is to cacth them and try
to re-open the connection. As Bill mentioned there is no way to know for
sure whether the connection is ok or not.
However, I suggest that one should always use the approach to open as late
as possible and close asap the connection.

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

John said:
So if it is closed for anyone reason it can be re-opened before executing
a query. Sort of trying to avoid the above mentioned error.

Regards

Miha Markic said:
Why do you need to check its status anyway?

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

John said:
Hi

I am creating a db connection using the below code. Is there a way to
check if the connection is open?

Thanks

Regards


= Code Below ====================

Dim LocalConn As OleDb.OleDbConnection
Dim St As String = My.Settings.LocalDBConnectionString

LocalConn = New OleDb.OleDbConnection(St)
LocalConn.Open()
 
sorry kid

..NET 'automagically closes connections for you'


I think that this is the biggest load of crap ever.
we can no longer use @@SPID as a database unique identifier

just because MS wanted to shove their premise (that keeping
connections closed is a good thing) down our throats

I call that EGOCENTRISM

in ADO Classic we can either share a connection or leave it open.

ADO.net is a failure because it has less funcitonality than ADO
Classic
 
Bill

that is a load of crap and it is a bug

stfu and make microsoft fix it
in ADO classic we could either leave a connecetion open; or we could
close it-- we could share it with other users-- things were peachy

ADO.net is crap
you can't have multiple data readers on the same connection;
screw .NET
 
Back
Top