Re: Close() and Dispose()

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Val Mazur (group51a_at_hotmail.com)
Date: 02/26/05


Date: Fri, 25 Feb 2005 21:16:58 -0500

Hi,

Dispose should close connection as well, but main purpose of Dispose not to
close the connection but to release the resources for the unmanaged code
inside of the class. If you do not call Dispose method you could face
situation when resources are not freed until you close your application. It
could lead to the fact when memory will grow and will not be released even
if you do not use objects anymore. Garbage Collector will not release memory
just because it does not know anything about unmanaged resources.

-- 
Val Mazur
Microsoft MVP
http://xport.mvps.org
"alx" <alx@icnsoftware.com> wrote in message 
news:1109323147.659445.56890@f14g2000cwb.googlegroups.com...
> On some documentation about ADO.NET, I've found that, during the close
> of a IDbConnection, is a "best practice" to close a connection with
> these lines of code:
>
>    connection.Close();
>    connection.Dispose();
>
> For example, Microsoft Data Access Application Block uses this code to
> close connection. Also they call Dispose method for others ADO.NET
> objects (Command, DataAdapter).
>
> I'd like to know if it's really necessary to close a Connection always
> with this 2 lines of code and call Dispose method for Command or
> DataAdapter objects.
>
> Thanks.
> 


Relevant Pages

  • Re: How can I make sure all unmanaged resources had been released ?
    ... SqlClient.SqlConnection should manually call Closeor Dispose() after ... SqlConnecont won't be closed when the connection object is out of range, ... > What type of unmanaged resources are you refering too? ... > Therefore if the unmanaged code is not desroying its objects, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: 2 ways to close a connection
    ... What other resources exactly does it release? ... It only closes the connection. ... > Calling the Dispose method of a SqlConnection object in turn calls Close ... > unmanaged resources used by the object. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: VS2005 and SQL Express: Internal .Net Framework Data Provider erro
    ... THe interal connection is underneath the ... public void CreateConnection{ ... // Close first and dispose the existing data reader is there is ... release resources used by the object ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: VS2005 and SQL Express: Internal .Net Framework Data Provider
    ... connection object you use. ... public void CreateConnection{ ... // Close first and dispose the existing data reader is there ... release resources used by the object ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: C# and GDI Resources
    ... interface use unmanaged resources; i.e. they end up being a wrapper to ... The rule is, if the object has a Dispose or Close method, make SURE you ... your class opens a database connection when an object is instanciated ... method would make sure to Dispose the database connection. ...
    (microsoft.public.dotnet.languages.csharp)