469,909 Members | 1,738 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Regarding data access, what should I dispose?

I recently discovered the "using" C# keyword and the neat and tidy way it
guarantees cleanup of resources. However, how far should I take this? I
recall some discussion from Microsoft suggesting that, in most cases, a
class's Dispose method should not be explicitly called, as it may introduce
unnecessary overhead.

For example, would this extreme be considered a best practice?

using (SqlConnection connection = new SqlConnection)
{
using (SqlCommand cmd = new SqlCommand)
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
... do stuff here ...
}
}
}

or is this sufficient?

using (SqlConnection connection = new SqlConnection)
{
SqlCommand cmd ...
SqlDataReader dr ...
}

or something in-between?

Regards,
Michael Carr
May 16 '06 #1
2 1134
See
http://blogs.msdn.com/brada/archive/...03/274718.aspx

IDataReader idr = null;
try {
//do stuff with the datareader here

} finally {
if ((!((idr == null)))) {
if ((!(idr.IsClosed))) {
idr.Close();
}
}
}
using is good for unmanaged resources.

I use the syntax from Brad Abrams blog.

Others can chime in opinions, I don't think mine is the only one.

...


"Michael Carr" <mc***@umich.edu> wrote in message
news:Oi**************@TK2MSFTNGP04.phx.gbl...
I recently discovered the "using" C# keyword and the neat and tidy way it
guarantees cleanup of resources. However, how far should I take this? I
recall some discussion from Microsoft suggesting that, in most cases, a
class's Dispose method should not be explicitly called, as it may introduce unnecessary overhead.

For example, would this extreme be considered a best practice?

using (SqlConnection connection = new SqlConnection)
{
using (SqlCommand cmd = new SqlCommand)
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
... do stuff here ...
}
}
}

or is this sufficient?

using (SqlConnection connection = new SqlConnection)
{
SqlCommand cmd ...
SqlDataReader dr ...
}

or something in-between?

Regards,
Michael Carr

May 16 '06 #2
In fact any object having a dispose() might be a critical one.
I really had trouble with connectionobjects.
Good thig is when you close a connection object, a datareader connected is
closed as well.
it seems a dr is not a serious issue, note the windows event log on failing
connections!!

I'm using a wrapper class which does dispose at some point all important
parts.
Of course i try to dispose by coding as well, at least i have almost no
events anymore.
"Michael Carr" <mc***@umich.edu> schreef in bericht
news:Oi**************@TK2MSFTNGP04.phx.gbl...
I recently discovered the "using" C# keyword and the neat and tidy way it
guarantees cleanup of resources. However, how far should I take this? I
recall some discussion from Microsoft suggesting that, in most cases, a
class's Dispose method should not be explicitly called, as it may introduce
unnecessary overhead.

For example, would this extreme be considered a best practice?

using (SqlConnection connection = new SqlConnection)
{
using (SqlCommand cmd = new SqlCommand)
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
... do stuff here ...
}
}
}

or is this sufficient?

using (SqlConnection connection = new SqlConnection)
{
SqlCommand cmd ...
SqlDataReader dr ...
}

or something in-between?

Regards,
Michael Carr

May 16 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Doug Bell | last post: by
7 posts views Thread by Mrinal Kamboj | last post: by
4 posts views Thread by George | last post: by
12 posts views Thread by joaotsetsemoita | last post: by
6 posts views Thread by Wesley Peace | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.