Regarding data access, what should I dispose?

M

Michael Carr

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
 
E

Edwin Knoppert

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top