Home All Groups Group Topic Archive Search About

Checking db connection open status

Author
3 May 2007 4:01 PM
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()

Author
3 May 2007 4:08 PM
rowe_newsgroups
Show quote Hide quote
On May 3, 12:01 pm, "John" <J...@nospam.infovis.co.uk> wrote:
> 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
Author
3 May 2007 4:14 PM
Patrice
Try :
http://msdn2.microsoft.com/en-us/library/system.data.oledb.oledbconnection.state(VS.71).aspx

If you don't have a local documentation, you'll find the class library
reference at :
http://msdn2.microsoft.com/en-us/library/ms229335.aspx

---
Patrice

"John" <John@nospam.infovis.co.uk> a écrit dans le message de news:
enIrbxZjHHA.4***@TK2MSFTNGP02.phx.gbl...
Show quoteHide quote
> 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()
>
>
Author
3 May 2007 5:59 PM
William (Bill) Vaughn
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)
-----------------------------------------------------------------------------------------------------------------------

Show quoteHide quote
"John" <John@nospam.infovis.co.uk> wrote in message
news:enIrbxZjHHA.4188@TK2MSFTNGP02.phx.gbl...
> 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()
>
>
Author
3 May 2007 9:42 PM
Miha Markic
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/

Show quoteHide quote
"John" <John@nospam.infovis.co.uk> wrote in message
news:enIrbxZjHHA.4188@TK2MSFTNGP02.phx.gbl...
> 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()
>
>
Author
4 May 2007 12:21 AM
John
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

Show quoteHide quote
"Miha Markic" <miha at rthand com> wrote in message
news:FD6A24A0-1861-45F7-838E-3F6986707AE2@microsoft.com...
> 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" <John@nospam.infovis.co.uk> wrote in message
> news:enIrbxZjHHA.4188@TK2MSFTNGP02.phx.gbl...
>> 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()
>>
>>
>
Author
4 May 2007 9:21 AM
Miha Markic
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/

Show quoteHide quote
"John" <John@nospam.infovis.co.uk> wrote in message
news:%2388azIejHHA.5084@TK2MSFTNGP03.phx.gbl...
> 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" <miha at rthand com> wrote in message
> news:FD6A24A0-1861-45F7-838E-3F6986707AE2@microsoft.com...
>> 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" <John@nospam.infovis.co.uk> wrote in message
>> news:enIrbxZjHHA.4188@TK2MSFTNGP02.phx.gbl...
>>> 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()
>>>
>>>
>>
>
>