473,378 Members | 1,504 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,378 software developers and data experts.

is Connection.close() enough or

do I need to explicitly do a

Connection.Dispose() too?

Thnx
Jun 27 '06 #1
6 1333
There's a load question (loaded in the sense that it can spark a debate).

In practice, your safe doing just .Close().

But, your asking for trouble not disposing of objects that implement
IDisposable. Point in case, SqlConnection's Dispose in 2.0 seems to do more
than in 1.x (I still don't think it _has_ to be called)..but who knows what
3.0 will do, and do you really want to go back through all your code and
change it then?
Generally, ur better off just calling Dispose() (which calls close) and not
calling close. Or, use "using"

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"mavrick_101" <ma********@discussions.microsoft.com> wrote in message
news:3D**********************************@microsof t.com...
do I need to explicitly do a

Connection.Dispose() too?

Thnx

Jun 27 '06 #2
Thanks for your response, Karl.

So equivalent of
connection.close();
connection.dispose();

would be
connection.dispose(); and that would close the conneciton too..

Cool.

"Karl Seguin [MVP]" wrote:
There's a load question (loaded in the sense that it can spark a debate).

In practice, your safe doing just .Close().

But, your asking for trouble not disposing of objects that implement
IDisposable. Point in case, SqlConnection's Dispose in 2.0 seems to do more
than in 1.x (I still don't think it _has_ to be called)..but who knows what
3.0 will do, and do you really want to go back through all your code and
change it then?
Generally, ur better off just calling Dispose() (which calls close) and not
calling close. Or, use "using"

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"mavrick_101" <ma********@discussions.microsoft.com> wrote in message
news:3D**********************************@microsof t.com...
do I need to explicitly do a

Connection.Dispose() too?

Thnx


Jun 27 '06 #3
Connection.Dispose calls Close. the main difference, is calling Displose
will release any event handler references. the best approach is to not call
either, but use a using statement:

using (SqlConnection conn = new SqlConnection())
{
// my code here
}

this will guarantee that the connection dispose is called, even of you have
an error, or exit the routine. as its a bad practice to pass a sqlconnection
between methods, this will keep you honest.

-- bruce (sqlwork.com)
"mavrick_101" <ma********@discussions.microsoft.com> wrote in message
news:3D**********************************@microsof t.com...
do I need to explicitly do a

Connection.Dispose() too?

Thnx

Jun 27 '06 #4
Get reflector (free) from http://www.aisto.com/roeder/dotnet/ and open up
the code yourself...it's an insanely useful tool. Make sure you have it set
to dissassemble to C# or VB.NET...and not IL :)

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"mavrick_101" <ma********@discussions.microsoft.com> wrote in message
news:B3**********************************@microsof t.com...
Thanks for your response, Karl.

So equivalent of
connection.close();
connection.dispose();

would be
connection.dispose(); and that would close the conneciton too..

Cool.

"Karl Seguin [MVP]" wrote:
There's a load question (loaded in the sense that it can spark a debate).

In practice, your safe doing just .Close().

But, your asking for trouble not disposing of objects that implement
IDisposable. Point in case, SqlConnection's Dispose in 2.0 seems to do
more
than in 1.x (I still don't think it _has_ to be called)..but who knows
what
3.0 will do, and do you really want to go back through all your code and
change it then?
Generally, ur better off just calling Dispose() (which calls close) and
not
calling close. Or, use "using"

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"mavrick_101" <ma********@discussions.microsoft.com> wrote in message
news:3D**********************************@microsof t.com...
> do I need to explicitly do a
>
> Connection.Dispose() too?
>
> Thnx


Jun 27 '06 #5
Yes.

I use the "using" keyword, which gives neat code, and ensures that the
connection is always closed, whatever happens:

using (SqlConnection connecton = new SqlConnection(...)) {
...
...
} // when leaving here, the connection is disposed

The code that is really created is a try...finally structure with the
dispose call in the finally section. It also copies the reference to the
object, so even if you fumble badly and lose your reference to the
object, it still gets disposed. :)
mavrick_101 wrote:
Thanks for your response, Karl.

So equivalent of
connection.close();
connection.dispose();

would be
connection.dispose(); and that would close the conneciton too..

Cool.

"Karl Seguin [MVP]" wrote:
There's a load question (loaded in the sense that it can spark a debate).

In practice, your safe doing just .Close().

But, your asking for trouble not disposing of objects that implement
IDisposable. Point in case, SqlConnection's Dispose in 2.0 seems to do more
than in 1.x (I still don't think it _has_ to be called)..but who knows what
3.0 will do, and do you really want to go back through all your code and
change it then?
Generally, ur better off just calling Dispose() (which calls close) and not
calling close. Or, use "using"

Karl

--
http://www.openmymind.net/
http://www.fuelindustries.com/
"mavrick_101" <ma********@discussions.microsoft.com> wrote in message
news:3D**********************************@microsof t.com...
do I need to explicitly do a

Connection.Dispose() too?

Thnx


Jun 27 '06 #6
It makes more sense now.

Thnx

"mavrick_101" wrote:
do I need to explicitly do a

Connection.Dispose() too?

Thnx

Jun 28 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: sam | last post by:
Hello group, I have a function which is used to initiate sqlDataReader object. I was trying to invoke the close method on the DataReader object but cant really do that as the function returns a...
5
by: Haydnw | last post by:
Hi, I have the code below as code-behind for a page which displays two images. My problem is with the second bit of code, commented as " 'Portfolio image section". Basically, the SQL query gets...
2
by: Bryan | last post by:
Hello, I'm just starting to develop in asp.net and i have a question about using a database connection globally in my app. I have set up the procedures for getting all my connection string info...
6
by: sri_san | last post by:
Hello, I have a bunch of similar functions as listed below. This one returns a string but lot of functions return datareader. I close the reader, the connection and set it to nothing but the...
5
by: DaM | last post by:
Hi guys, I'm having this problem with my ASP.Net application: I was testing the whole site, and it seem to work fine. Fast and stable, but suddenly it stopped working and this error occurred:...
35
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing...
16
by: crbd98 | last post by:
Hello All, Some time ago, I implemented a data access layer that included a simple connectin pool. At the time, I did it all by myself: I created N connections, each connection associated with...
20
by: fniles | last post by:
I am using VS2003 and connecting to MS Access database. When using a connection pooling (every time I open the OLEDBCONNECTION I use the exact matching connection string), 1. how can I know how...
5
by: This | last post by:
I have a pretty basic emailing script that sends a relatively small number (150) of html emails. The emails are compiled, personalised from a mysql db subscribers list, and sent using mail() -...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.