473,287 Members | 1,834 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,287 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 1245
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...

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.