473,386 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 1249
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
6
by: TB | last post by:
I understand the basics of finalization and implementing IDisposable and how one is guaranteed access to managed objects only when working through the IDisposable interface. My question is to what...
5
by: Edward W. | last post by:
I was looking at the data access application block version 2 and considering what I had read about connection pooling in that to use connection pooling successfully, you should close your...
10
by: Doug Bell | last post by:
Hi, I have an application that has a "Data Access Class" and "User Interface Class". It is for receiving Purchase Order data from one system and pushing processed transactions to another...
7
by: Mrinal Kamboj | last post by:
Hi , I am using OracleConnection object from Oracle ODP.net provider and following is the behaviour which i am finding bit strange : To start with my argument is based on followings facts : ...
4
by: George | last post by:
Hi all, I am having trouble with updating my data in an Access database. here is my code: Imports System.Data.OleDb Dim AppPath As String = Mid(Application.ExecutablePath, 1,...
6
Cintury
by: Cintury | last post by:
Hi all, I've developed a mobile application for windows mobile 5.0 that has been in use for a while (1 year and a couple of months). It was developed in visual studios 2005 with a back-end sql...
12
by: joaotsetsemoita | last post by:
Hello everyone, im completly new to vb.net and I was assigned to do a simple piece of software that just had to select from um db in a MS access data base and insert into a SQL server Database....
6
by: Wesley Peace | last post by:
I hate to cross post, but I've gotten no answer yet on a problem I'm having with visual studio 2008. I've created a series of forms with controls to access a Access database tables. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.