473,386 Members | 1,706 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.

close db connections

using (SqlConnection connection = new SqlConnection(constr))
{
using (SqlCommand command = new SqlCommand(com, connection))
{
//read from db
}
}
Is this sufficient to close db connections or do I need to add
connection.close()..

I know the object will be disposed after this statement but will it close
open db connections?

What is the best practice for running a sql query in asp.net

Thanks,
Howard
Apr 10 '06 #1
3 1250
Hi Howard,

Dispose() will close your connection so there's not need to Close() as
well. Unlike Close(), Dispose() will also clear the connection's
ConnectionString, so be careful if you try and Open() it again.

HTH,

Chris

Apr 10 '06 #2
Correct.

I would add that the Command object also needs
to be disposed of, not only the Connection object.

So, not only is connection.close() needed,
but command.close() is needed, too.


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Chris Fulstow" <ch**********@hotmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Hi Howard,

Dispose() will close your connection so there's not need to Close() as
well. Unlike Close(), Dispose() will also clear the connection's
ConnectionString, so be careful if you try and Open() it again.

HTH,

Chris

Apr 10 '06 #3
re:
So, not only is connection.close() needed,
but command.close() is needed, too.
That should have been command.Dispose()

Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Juan T. Llibre" <no***********@nowhere.com> wrote in message
news:uP**************@TK2MSFTNGP05.phx.gbl... Correct.

I would add that the Command object also needs
to be disposed of, not only the Connection object.

So, not only is connection.close() needed,
but command.close() is needed, too.


Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Chris Fulstow" <ch**********@hotmail.com> wrote in message
news:11*********************@g10g2000cwb.googlegro ups.com...
Hi Howard,

Dispose() will close your connection so there's not need to Close() as
well. Unlike Close(), Dispose() will also clear the connection's
ConnectionString, so be careful if you try and Open() it again.

HTH,

Chris


Apr 10 '06 #4

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

Similar topics

2
by: Lin Ma | last post by:
Hello, I have a general question. In my asp page, I have DB connection, Recordset, and some variables like dim name, conn, rs set conn = Server.CreateObject("ADODB.Connection") .... set rs=...
9
by: AA | last post by:
This is making me crazy!! Please, if some body can help me. I'm testing a ver simple socket client. In my test I just open and close a connection (in a loop) to my local IIS server (port 80)...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
8
by: Pierson C | last post by:
I am developing on a website that is utilizing SQL Server 2000. Shortly after deploying the site, we began having timeout issues due to the max connections. 1st instinct was to diligently tidy...
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...
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:
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
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.