Main Page | Report this Page
.NET DotNet Forum Index  »  ASP.NET Forum  »  "Close connection" or "Dispose connection"...
Page 1 of 1    

"Close connection" or "Dispose connection"...

Author Message
aspfun via DotNetMonster.com...
Posted: Wed Oct 28, 2009 8:50 am
Guest
After running update store procedure(using ado.net), do I need add "close
connection" or "dispose connection" or need both for System.Data.SqlClient.
SqlConnection?

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/asp-net/200910/1
 
Scott M....
Posted: Wed Oct 28, 2009 10:01 am
Guest
"aspfun via DotNetMonster.com" <u53138 at (no spam) uwe> wrote in message
news:9e45845cf2bb6 at (no spam) uwe...
Quote:
After running update store procedure(using ado.net), do I need add "close
connection" or "dispose connection" or need both for
System.Data.SqlClient.
SqlConnection?

--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/asp-net/200910/1

Connections need to be closed so that they don't tie up a connection that
another application might want, so you should close your connection as soon
as your code has gotten the data it needs. Closing a connection doesn't
destroy it though and so you could re-open it later if needed.

However, just because a connection is closed doesn't mean that the link
between your application's memroy and the database is broken. Dispose is a
very common method that any class that uses unmanaged resources (and even
managed for that matter) may have. You should call Dispose when your
application no longer will need the connection. This tells the .NET
Framework to perform any cleanup code that the type has built into it.
Calling Dispose on an open connection will cause it to close and dispose.

Or, you could just use the "Using" block construct, which automatically
disposes of your object when you hit the end using.

-Scot
 
bruce barker...
Posted: Wed Oct 28, 2009 8:26 pm
Guest
either one. use Close, if you want to reopen. Dispose just calls close
in its finalizer. the best practice is to use a using statement then you
don't have to remember to call either.

-- bruce (sqlwork.com)

aspfun via DotNetMonster.com wrote:
Quote:
After running update store procedure(using ado.net), do I need add "close
connection" or "dispose connection" or need both for System.Data.SqlClient.
SqlConnection?
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Sat Dec 12, 2009 1:27 am