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> </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" <<A=20
=
href=3D"mailto:inb4dalockNOSPAM@hotmail.com">inb4dalockNOSPAM@hotmail.com=
</A>>=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> //create & open a=20
SqlConnection, and dispose of it after we are =
done.<BR> =20
using (SqlConnection cn =3D new=20
SqlConnection(connectionString))<BR> =
{<BR> =20
cn.Open();<BR> =
//call=20
the overload that takes a connection in place of the connection=20
string<BR> return =
ExecuteNonQuery(cn,=20
commandType, commandText, commandParameters);<BR> =20
}<BR>}<BR></FONT><BR><BR>"Miha Markic [MVP C#]" <miha at rthand =
com>=20
wrote in message news:uDgYiD26DHA.2412@TK2MSFTNGP09.phx.gbl...<BR>> =
Hi,<BR>> <BR>> Depends on the provider. SqlConnection doesn't =
close when=20
finalized.<BR>> And there is another problem - even if it is closed =
you=20
won't never know<BR>> when it closes because it is in hands of =
garbage=20
collection.<BR>> Bottom point:<BR>> Always, close the connection =
explicitly asap (put Close in finally part of<BR>> =
try/finally).<BR>>=20
<BR>> -- <BR>> Miha Markic [MVP C#] - RightHand .NET consulting =
&=20
development<BR>> miha at rthand com<BR>> www.rthand.com<BR>> =
<BR>>=20
"Twitch" <inb4dalockNOSPAM@hotmail.com> wrote in message<BR>> =
news:rNcUb.3969$213.499@edtnps89...<BR>> > WRT:<BR>> >=20
System.Data.SqlClient.SqlConnection<BR>> ><BR>> > When an=20
SqlConnection goes out of scope, is the .close() called by =
it's<BR>> >=20
destructor?<BR>> ><BR>> > thanks.<BR>> ><BR>>=20
><BR>> <BR>> </FONT></BLOCKQUOTE></BODY></HTML>
------=_NextPart_000_002A_01C3EB3F.77687FB0--