473,320 Members | 2,024 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,320 software developers and data experts.

using(SqlConnection = ... ) ?

I've noticed that if I use a statement lik
using(StreamWriter sw = new StreamWriter(@"c:\filetowriteto.txt")
{...

then the file gets released at the end of the using statement block
However if I use this on a database connection
using(SqlConnection cndb = new SqlConnection(@"....")
{...
then unless I explicitly call Close, it doesn't close it. Certainly not with SQ
server connections anyway, they're still visible in Enterprise manager unti
you call close
What is it with IDisposable - what does it actually DO? Is there any goo
description of what resources it frees and what it doesn't
Can you derive from it to define exactly what resources you want to free

Nov 16 '05 #1
3 2520
sevenfifteen <an*******@discussions.microsoft.com> wrote:
I've noticed that if I use a statement like
using(StreamWriter sw = new StreamWriter(@"c:\filetowriteto.txt"))
{...}

then the file gets released at the end of the using statement block.
Yup.
However if I use this on a database connection,
using(SqlConnection cndb = new SqlConnection(@"...."))
{...}
then unless I explicitly call Close, it doesn't close it. Certainly not with SQL
server connections anyway, they're still visible in Enterprise manager until
you call close.
It will still be available if you call Close, too - and quite
deliberately. Connections to SQL Server are pooled so that you can
create new SqlConnection objects and open and close them very often
without taking up much in the way of actual resources. A SqlConnection
doesn't represent the *real* connection to the database - it just wraps
it, effectively.
What is it with IDisposable - what does it actually DO? Is there any good
description of what resources it frees and what it doesn't?


It releases unmanaged resources. In this case, the unmanaged resource
is the fact that the connection is in use - it only returns it to the
pool.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
If you call Close() on an SqlConnection, then that makes it disappear from sp_who
or Enterprise Manager. i.e. you're actually telling SQL server that you don't want to us
that connection any more
If you let a using block expire, it doesn't
Damn.
Nov 16 '05 #3
sevenfifteen <an*******@discussions.microsoft.com> wrote:
If you call Close() on an SqlConnection, then that makes it disappear from sp_who2
or Enterprise Manager. i.e. you're actually telling SQL server that
you don't want to use that connection any more.
If you let a using block expire, it doesn't.
Damn.


It certainly doesn't log out the connection - or at least, it didn't
when I last tried it, looking at SQL Server Profiler. I suggest you use
the profiler to see what happens - I've got a test program in the
..adonet newsgroup from about a week ago asking about the same question.

In fact, I've just tried executing sp_who2 myself with my test program,
after opening and closing a connection 3 times - and I see two sleeping
connections. It's only after the process exits that the connections
drop.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

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

Similar topics

1
by: Ramesh | last post by:
hi, Can we use SQLConnection object to connect to oracle? I understand that for connecting Oracle we have to use only ODP.net (or) OLEDB Object. Is it correct? Please... Thanks, Ramesh
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...
2
by: R.A.M. | last post by:
Hello, I have started larning C# and I have a question concerning "using (...)" keyword. For example: using (SqlConnection connection = new SqlConnection(ConnectionString)) {...
2
by: Tony Johansson | last post by:
Hello! Just a simple question is it good to always close a sqlConnection even if the Connection has been closed by the DataAdapter ? //Tony
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.