WRT:
System.Data.SqlClient.SqlConnection

When an SqlConnection goes out of scope, is the .close() called by it's
destructor?

thanks.

Re: When a connection closes.... by Miha

Miha
Wed Feb 04 14:52:37 CST 2004

Hi,

Depends on the provider. SqlConnection doesn't close when finalized.
And there is another problem - even if it is closed you won't never know
when it closes because it is in hands of garbage collection.
Bottom point:
Always, close the connection explicitly asap (put Close in finally part of
try/finally).

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

"Twitch" <inb4dalockNOSPAM@hotmail.com> wrote in message
news:rNcUb.3969$213.499@edtnps89...
> WRT:
> System.Data.SqlClient.SqlConnection
>
> When an SqlConnection goes out of scope, is the .close() called by it's
> destructor?
>
> thanks.
>
>



Re: When a connection closes.... by Twitch

Twitch
Wed Feb 04 15:06:02 CST 2004

This is a multi-part message in MIME format.

------=_NextPart_000_0041_01C3EB38.85FC8280
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

So this little snippet from DataAccess Application block is bad mojo?

public static int ExecuteNonQuery(string connectionString, CommandType =
commandType, string commandText, params SqlParameter[] =
commandParameters)
{
//create & open a SqlConnection, and dispose of it after we are =
done.
using (SqlConnection cn =3D new SqlConnection(connectionString))
{
cn.Open();
//call the overload that takes a connection in place of the =
connection string
return ExecuteNonQuery(cn, commandType, commandText, =
commandParameters);
}
}


"Miha Markic [MVP C#]" <miha at rthand com> wrote in message =
news:uDgYiD26DHA.2412@TK2MSFTNGP09.phx.gbl...
> Hi,
>=20
> Depends on the provider. SqlConnection doesn't close when finalized.
> And there is another problem - even if it is closed you won't never =
know
> when it closes because it is in hands of garbage collection.
> Bottom point:
> Always, close the connection explicitly asap (put Close in finally =
part of
> try/finally).
>=20
> --=20
> Miha Markic [MVP C#] - RightHand .NET consulting & development
> miha at rthand com
> www.rthand.com
>=20
> "Twitch" <inb4dalockNOSPAM@hotmail.com> wrote in message
> news:rNcUb.3969$213.499@edtnps89...
> > WRT:
> > System.Data.SqlClient.SqlConnection
> >
> > When an SqlConnection goes out of scope, is the .close() called by =
it's
> > destructor?
> >
> > thanks.
> >
> >
>=20
>
------=_NextPart_000_0041_01C3EB38.85FC8280
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1170" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff background=3D""><FONT face=3DArial size=3D2>So =
this little=20
snippet from DataAccess Application block is bad mojo?<BR><BR><FONT=20
face=3D"Lucida Console">public static int ExecuteNonQuery(string =
connectionString,=20
CommandType commandType, string commandText, params SqlParameter[]=20
commandParameters)<BR>{<BR>&nbsp;&nbsp;&nbsp; //create &amp; open a=20
SqlConnection, and dispose of it after we are =
done.<BR>&nbsp;&nbsp;&nbsp; using=20
(SqlConnection cn =3D new =
SqlConnection(connectionString))<BR>&nbsp;&nbsp;&nbsp;=20
{<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
cn.Open();<BR>&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; //call the overload that takes a connection in place =
of the=20
connection string<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return=20
ExecuteNonQuery(cn, commandType, commandText,=20
commandParameters);<BR>&nbsp;&nbsp;&nbsp; }<BR>}<BR></FONT><BR><BR>"Miha =
Markic=20
[MVP C#]" &lt;miha at rthand com&gt; wrote in message=20
news:uDgYiD26DHA.2412@TK2MSFTNGP09.phx.gbl...<BR>&gt; Hi,<BR>&gt; =
<BR>&gt;=20
Depends on the provider. SqlConnection doesn't close when =
finalized.<BR>&gt; And=20
there is another problem - even if it is closed you won't never =
know<BR>&gt;=20
when it closes because it is in hands of garbage collection.<BR>&gt; =
Bottom=20
point:<BR>&gt; Always, close the connection explicitly asap (put Close =
in=20
finally part of<BR>&gt; try/finally).<BR>&gt; <BR>&gt; -- <BR>&gt; Miha =
Markic=20
[MVP C#] - RightHand .NET consulting &amp; development<BR>&gt; miha at =
rthand=20
com<BR>&gt; www.rthand.com<BR>&gt; <BR>&gt; "Twitch"=20
&lt;inb4dalockNOSPAM@hotmail.com&gt; wrote in message<BR>&gt;=20
news:rNcUb.3969$213.499@edtnps89...<BR>&gt; &gt; WRT:<BR>&gt; &gt;=20
System.Data.SqlClient.SqlConnection<BR>&gt; &gt;<BR>&gt; &gt; When an=20
SqlConnection goes out of scope, is the .close() called by it's<BR>&gt; =
&gt;=20
destructor?<BR>&gt; &gt;<BR>&gt; &gt; thanks.<BR>&gt; &gt;<BR>&gt; =
&gt;<BR>&gt;=20
<BR>&gt; </FONT></BODY></HTML>

------=_NextPart_000_0041_01C3EB38.85FC8280--


Re: When a connection closes.... by William

William
Wed Feb 04 18:53:51 CST 2004

This is a multi-part message in MIME format.

------=_NextPart_000_002A_01C3EB3F.77687FB0
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Nope. It's ok. Note that the Using operator automatically calls Dispose =
on objects created in its scope. Dispose calls Close.=20
No, I don't like this approach because it causes perfectly sane =
developers to ask questions (good questions) like this. I prefer to call =
Close explicitly--but with this syntax there was no place to put a =
Close.

This operator comes to VB.NET in the Whidbey timeframe. I guess we had =
(all) be prepared to understand its use. ;)

--=20
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.=20
This posting is provided "AS IS" with no warranties, and confers no =
rights.
__________________________________

"Twitch" <inb4dalockNOSPAM@hotmail.com> wrote in message =
news:_8dUb.3974$213.322@edtnps89...
So this little snippet from DataAccess Application block is bad mojo?

public static int ExecuteNonQuery(string connectionString, CommandType =
commandType, string commandText, params SqlParameter[] =
commandParameters)
{
//create & open a SqlConnection, and dispose of it after we are =
done.
using (SqlConnection cn =3D new SqlConnection(connectionString))
{
cn.Open();
//call the overload that takes a connection in place of the =
connection string
return ExecuteNonQuery(cn, commandType, commandText, =
commandParameters);
}
}


"Miha Markic [MVP C#]" <miha at rthand com> wrote in message =
news:uDgYiD26DHA.2412@TK2MSFTNGP09.phx.gbl...
> Hi,
>=20
> Depends on the provider. SqlConnection doesn't close when finalized.
> And there is another problem - even if it is closed you won't never =
know
> when it closes because it is in hands of garbage collection.
> Bottom point:
> Always, close the connection explicitly asap (put Close in finally =
part of
> try/finally).
>=20
> --=20
> Miha Markic [MVP C#] - RightHand .NET consulting & development
> miha at rthand com
> www.rthand.com
>=20
> "Twitch" <inb4dalockNOSPAM@hotmail.com> wrote in message
> news:rNcUb.3969$213.499@edtnps89...
> > WRT:
> > System.Data.SqlClient.SqlConnection
> >
> > When an SqlConnection goes out of scope, is the .close() called by =
it's
> > destructor?
> >
> > thanks.
> >
> >
>=20
>
------=_NextPart_000_002A_01C3EB3F.77687FB0
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1400" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff background=3D"">
<DIV><FONT size=3D2>Nope. It's ok. Note that the Using operator =
automatically=20
calls Dispose on objects created in its scope. Dispose calls Close.=20
</FONT></DIV>
<DIV><FONT size=3D2>No, I don't like this approach because it causes =
perfectly=20
sane developers to ask questions (good questions) like this. I prefer to =
call=20
Close explicitly--but with this syntax there was no place to put a=20
Close.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>This operator comes to VB.NET in the Whidbey =
timeframe. I=20
guess we had (all) be prepared to understand its use. ;)</FONT></DIV>
<DIV><BR>-- <BR>____________________________________<BR>William (Bill)=20
Vaughn<BR>Author, Mentor, Consultant<BR>Microsoft MVP<BR><A=20
href=3D"http://www.betav.com">www.betav.com</A><BR>Please reply only to =
the=20
newsgroup so that others can benefit. <BR>This posting is provided "AS =
IS" with=20
no warranties, and confers no=20
rights.<BR>__________________________________<BR></DIV>
<BLOCKQUOTE=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV>"Twitch" &lt;<A=20
=
href=3D"mailto:inb4dalockNOSPAM@hotmail.com">inb4dalockNOSPAM@hotmail.com=
</A>&gt;=20
wrote in message <A=20
=
href=3D"news:_8dUb.3974$213.322@edtnps89">news:_8dUb.3974$213.322@edtnps8=
9</A>...</DIV><FONT=20
face=3DArial size=3D2>So this little snippet from DataAccess =
Application block is=20
bad mojo?<BR><BR><FONT face=3D"Lucida Console">public static int=20
ExecuteNonQuery(string connectionString, CommandType commandType, =
string=20
commandText, params SqlParameter[]=20
commandParameters)<BR>{<BR>&nbsp;&nbsp;&nbsp; //create &amp; open a=20
SqlConnection, and dispose of it after we are =
done.<BR>&nbsp;&nbsp;&nbsp;=20
using (SqlConnection cn =3D new=20
SqlConnection(connectionString))<BR>&nbsp;&nbsp;&nbsp; =
{<BR>&nbsp;&nbsp;&nbsp;=20
&nbsp;&nbsp;&nbsp; cn.Open();<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; =
//call=20
the overload that takes a connection in place of the connection=20
string<BR>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; return =
ExecuteNonQuery(cn,=20
commandType, commandText, commandParameters);<BR>&nbsp;&nbsp;&nbsp;=20
}<BR>}<BR></FONT><BR><BR>"Miha Markic [MVP C#]" &lt;miha at rthand =
com&gt;=20
wrote in message news:uDgYiD26DHA.2412@TK2MSFTNGP09.phx.gbl...<BR>&gt; =

Hi,<BR>&gt; <BR>&gt; Depends on the provider. SqlConnection doesn't =
close when=20
finalized.<BR>&gt; And there is another problem - even if it is closed =
you=20
won't never know<BR>&gt; when it closes because it is in hands of =
garbage=20
collection.<BR>&gt; Bottom point:<BR>&gt; Always, close the connection =

explicitly asap (put Close in finally part of<BR>&gt; =
try/finally).<BR>&gt;=20
<BR>&gt; -- <BR>&gt; Miha Markic [MVP C#] - RightHand .NET consulting =
&amp;=20
development<BR>&gt; miha at rthand com<BR>&gt; www.rthand.com<BR>&gt; =
<BR>&gt;=20
"Twitch" &lt;inb4dalockNOSPAM@hotmail.com&gt; wrote in message<BR>&gt; =

news:rNcUb.3969$213.499@edtnps89...<BR>&gt; &gt; WRT:<BR>&gt; &gt;=20
System.Data.SqlClient.SqlConnection<BR>&gt; &gt;<BR>&gt; &gt; When an=20
SqlConnection goes out of scope, is the .close() called by =
it's<BR>&gt; &gt;=20
destructor?<BR>&gt; &gt;<BR>&gt; &gt; thanks.<BR>&gt; &gt;<BR>&gt;=20
&gt;<BR>&gt; <BR>&gt; </FONT></BLOCKQUOTE></BODY></HTML>

------=_NextPart_000_002A_01C3EB3F.77687FB0--


Re: When a connection closes.... by bruce

bruce
Wed Feb 04 19:44:31 CST 2004

yes, but the destructor is not called until the GC get around to it (which
may not be until task exit). by convention, any object that uses unmanaged
resoures implements a Dispose method (and often a Close that calls it). This
allows code to release the unmanaged resources when done, rather than when
the GC needs to.

the GC only collects an object when it needs the memory, and has no way to
know that the object is hoarding critial unmanaged resources (like file or
sql connections).

C# supplies the "using" statement to make using this bad boys easier and
safer. if you don't use the "using" statement, than you should always use a
try/catch/finally and Dispose/Close the object in the finally clause.

-- bruce (sqlwork.com)

"Twitch" <inb4dalockNOSPAM@hotmail.com> wrote in message
news:rNcUb.3969$213.499@edtnps89...
> WRT:
> System.Data.SqlClient.SqlConnection
>
> When an SqlConnection goes out of scope, is the .close() called by it's
> destructor?
>
> thanks.
>
>



Re: When a connection closes.... by Angel

Angel
Thu Feb 05 16:39:57 CST 2004

I love the "using" construct, just think of it as
ry{ }finally{myUsedObject.Dispose() }.

To make it clear, the following two pieces of code are exactly the same:

====================
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open();
return ExecuteNonQuery(cn, commandType, commandText,
commandParameters);
}
====================================
SqlConnection cn=null;
try
{
cn = new SqlConnection(connectionString))
cn.Open();
return ExecuteNonQuery(cn, commandType, commandText,
commandParameters);
}
finally
{
cn.Dispose();
}
=====================

The benefit that both of these examples have is that Dispose will always get
called, even if an exception is thrown inside ExecuteNonQuery, since the
finally is guaranteed to run. If you don't have the finally then you would
not close the connection at all on exceptions, enough of those put together
and bad mojo will happen.


--
Angel Saenz-Badillos [MS] Managed Providers
This posting is provided "AS IS", with no warranties, and confers no
rights.Please do not send email directly to this alias.
This alias is for newsgroup purposes only.

"William (Bill) Vaughn" <billvaRemoveThis@nwlink.com> wrote in message
news:eiMAHK46DHA.2056@TK2MSFTNGP10.phx.gbl...
Nope. It's ok. Note that the Using operator automatically calls Dispose on
objects created in its scope. Dispose calls Close.
No, I don't like this approach because it causes perfectly sane developers
to ask questions (good questions) like this. I prefer to call Close
explicitly--but with this syntax there was no place to put a Close.

This operator comes to VB.NET in the Whidbey timeframe. I guess we had (all)
be prepared to understand its use. ;)

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
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.
__________________________________

"Twitch" <inb4dalockNOSPAM@hotmail.com> wrote in message
news:_8dUb.3974$213.322@edtnps89...
So this little snippet from DataAccess Application block is bad mojo?

public static int ExecuteNonQuery(string connectionString, CommandType
commandType, string commandText, params SqlParameter[] commandParameters)
{
//create & open a SqlConnection, and dispose of it after we are done.
using (SqlConnection cn = new SqlConnection(connectionString))
{
cn.Open();
//call the overload that takes a connection in place of the
connection string
return ExecuteNonQuery(cn, commandType, commandText,
commandParameters);
}
}


"Miha Markic [MVP C#]" <miha at rthand com> wrote in message
news:uDgYiD26DHA.2412@TK2MSFTNGP09.phx.gbl...
> Hi,
>
> Depends on the provider. SqlConnection doesn't close when finalized.
> And there is another problem - even if it is closed you won't never know
> when it closes because it is in hands of garbage collection.
> Bottom point:
> Always, close the connection explicitly asap (put Close in finally part
of
> try/finally).
>
> --
> Miha Markic [MVP C#] - RightHand .NET consulting & development
> miha at rthand com
> www.rthand.com
>
> "Twitch" <inb4dalockNOSPAM@hotmail.com> wrote in message
> news:rNcUb.3969$213.499@edtnps89...
> > WRT:
> > System.Data.SqlClient.SqlConnection
> >
> > When an SqlConnection goes out of scope, is the .close() called by
it's
> > destructor?
> >
> > thanks.
> >
> >
>
>