469,641 Members | 1,189 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,641 developers. It's quick & easy.

what are the ways to ensure a sql connection is close?

greetings, besides using objconn.State() to find out if a sql connection is
close or open, is there any other way to accomplish this task? thanks in
advance.
anywhere is sql server or the machine to tell this?
Nov 18 '05 #1
8 1296
You can call Dispose() on the SQlConnection.....this will ensure that the
connection is closed, and won't cause any harm if it already is (it itself
checks to see the state of the connection).

All you need to make sure is that you aren't calling dispose() on a null
reference (of course).

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Asha" <As**@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...
greetings, besides using objconn.State() to find out if a sql connection is close or open, is there any other way to accomplish this task? thanks in
advance.
anywhere is sql server or the machine to tell this?

Nov 18 '05 #2
What's wrong with the State property? There aren't many things in life that
are much much easier than this. What could be simpler than:

if ( objconn.State == ConnectionState.Closed )

???

Of course, if you want to know if it's NOT closed then I'm sure you know
that it would be:

if ( objConn.State != ConnectionState.Closed )
In considerable puzzlement,
Tom Dacon
Dacon Software Consulting

"Asha" <As**@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...
greetings, besides using objconn.State() to find out if a sql connection
is
close or open, is there any other way to accomplish this task? thanks in
advance.
anywhere is sql server or the machine to tell this?

Nov 18 '05 #3
thanks for the reply dacon... the .state works fine... but what i would like
to know is that, are they any other ways to tell if a connection is close?
e.g. from the sql server we could use sp_who *which i know your pretty
fimiliar with, how about other ways. can i tell it from the machine? thanks
dacon for the reply.

"Tom Dacon" wrote:
What's wrong with the State property? There aren't many things in life that
are much much easier than this. What could be simpler than:

if ( objconn.State == ConnectionState.Closed )

???

Of course, if you want to know if it's NOT closed then I'm sure you know
that it would be:

if ( objConn.State != ConnectionState.Closed )
In considerable puzzlement,
Tom Dacon
Dacon Software Consulting

"Asha" <As**@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...
greetings, besides using objconn.State() to find out if a sql connection
is
close or open, is there any other way to accomplish this task? thanks in
advance.
anywhere is sql server or the machine to tell this?


Nov 18 '05 #4
thanks for the reply dacon... the .state works fine... but what i would like
to know is that, are they any other ways to tell if a connection is close?
e.g. from the sql server we could use sp_who *which i know your pretty
fimiliar with, how about other ways. can i tell it from the machine? thanks
dacon for the reply.

"Tom Dacon" wrote:
What's wrong with the State property? There aren't many things in life that
are much much easier than this. What could be simpler than:

if ( objconn.State == ConnectionState.Closed )

???

Of course, if you want to know if it's NOT closed then I'm sure you know
that it would be:

if ( objConn.State != ConnectionState.Closed )
In considerable puzzlement,
Tom Dacon
Dacon Software Consulting

"Asha" <As**@discussions.microsoft.com> wrote in message
news:D7**********************************@microsof t.com...
greetings, besides using objconn.State() to find out if a sql connection
is
close or open, is there any other way to accomplish this task? thanks in
advance.
anywhere is sql server or the machine to tell this?


Nov 18 '05 #5
Why?

--
Girish Bharadwaj
http://msmvps.com/gbvb
"Asha" <As**@discussions.microsoft.com> wrote in message
news:D5**********************************@microsof t.com...
thanks for the reply dacon... the .state works fine... but what i would
like
to know is that, are they any other ways to tell if a connection is close?
e.g. from the sql server we could use sp_who *which i know your pretty
fimiliar with, how about other ways. can i tell it from the machine?
thanks
dacon for the reply.

<snip/>
Nov 18 '05 #6
"Girish bharadwaj" <gi*****@mvps.org> wrote in message news:<e7**************@TK2MSFTNGP11.phx.gbl>...
Why?

why why? :-)

There are many good reasons do look for open connections. use select *
from sysprocesses (master db) in Queryanalyzer and filter results for
yur dbid. If you dont know dbid use select * from sysdatabases where
name = 'mydb'.
I wish there was a way to break into the debugger when a new
connection is opened...that would make hunting for unclosed
connections much more easy.

There is also a way to use EM but this methods works beter for me (no
caching in UI)

regards
Mathias Fritsch
Nov 18 '05 #7
:) Ok. That was a bit cryptic. The question was answered in other places and
the OP asked for other ways to do this. Well, If the OPs question was not
answered by the answers here, I wanted to understand why this was needed so
that we might be able to come up with a better solution.

--
Girish Bharadwaj
http://msmvps.com/gbvb
"Matze" <ma************@gmx.de> wrote in message
news:24**************************@posting.google.c om...
"Girish bharadwaj" <gi*****@mvps.org> wrote in message

news:<e7**************@TK2MSFTNGP11.phx.gbl>...
Why?

why why? :-)

There are many good reasons do look for open connections. use select *
from sysprocesses (master db) in Queryanalyzer and filter results for
yur dbid. If you dont know dbid use select * from sysdatabases where
name = 'mydb'.
I wish there was a way to break into the debugger when a new
connection is opened...that would make hunting for unclosed
connections much more easy.

There is also a way to use EM but this methods works beter for me (no
caching in UI)

regards
Mathias Fritsch

Nov 18 '05 #8
Common practice for me is to put the Connection.Close() in the finally
block of my service layer. The service layer opens the connection and
passes it to the DAO layer for use. After the DAO layer retunrs to the
service layer the finally block will close it. However with data
readers that need to be passed back to the view or the code behind
this will not work. SO make sure to open all data readers with the
correct constructor =-)

B

"Girish bharadwaj" <gi*****@mvps.org> wrote in message news:<e7**************@TK2MSFTNGP11.phx.gbl>...
Why?

--
Girish Bharadwaj
http://msmvps.com/gbvb
"Asha" <As**@discussions.microsoft.com> wrote in message
news:D5**********************************@microsof t.com...
thanks for the reply dacon... the .state works fine... but what i would
like
to know is that, are they any other ways to tell if a connection is close?
e.g. from the sql server we could use sp_who *which i know your pretty
fimiliar with, how about other ways. can i tell it from the machine?
thanks
dacon for the reply.

<snip/>

Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by pradeep_TP | last post: by
35 posts views Thread by Eric Sabine | last post: by
3 posts views Thread by Frustrated Developer via DotNetMonster.com | last post: by
669 posts views Thread by Xah Lee | last post: by
16 posts views Thread by SLIMSHIM | last post: by
16 posts views Thread by HillBilly | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.