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[^]