Click here to Skip to main content
15,915,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello there

I jst want to know that..what will be the problem if we will not close the connection.

it may be simple question but Please help me

Thanks in Advance

Ramesh Ch tulal
Posted
Comments
wizardzz 15-Jul-11 12:16pm    
"More on" is very accurate.

1 solution

when we open an Connection it will responsible to hold all desired operation of databases in your application but what happens is when we do not close the connection explicitly. it will lead to Leaking Connection problem how take a look
When we do not close/dispose the connection, GC collects them in its own time, such connections are considered as leaked from pooling point of view. There is a strange possibility that we reach max pool size value and at that given moment of time without actually using all of them, having couple of them leaked and waiting for GC to work upon them. This would actually lead to the exception mentioned above, even if we are not using max pool size number of connections.

The answer to Such type of problems is the using statement. This statement guarantees that resources are properly cleaned up and that they're cleaned up in the most timely manner.

Recoded with the using statement our code block would look like:
using (SqlConnection connection = new SqlConnection(connectionString))
{
    
}


Reference Link :-
ADO.NET Connection Pooling at a Glance[^]
SqlConnection.Close Method[^]
 
Share this answer
 
v2
Comments
Abhinav S 15-Jul-11 13:04pm    
Good answer. My 5.
RaviRanjanKr 16-Jul-11 0:40am    
Thanks Abhinav :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900