0

Dear All,

I am writing a web application based on C#. What I would like to achieve is the following: an application that uses a single, global SQL connection. Which can be opened or closed when querying the db.

I think it would be nice to avoid instantiating an SqlConnection object every time I read some data from the db.

What is the best way to proceed?

Thanks!

gnl

2
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by gnl74
0

Hi,
I would avoid making the database connection global to your application. One way to achieve what you are after is to create a database that creates the connection for you.
Then when you need to access the database, instantiate the database class and use the connection it provides.
I wouldn't be too worried about the resource cost of doing this. Its better than passing around the same single instantiated connnection object.

0

Hi,
I would avoid making the database connection global to your application. One way to achieve what you are after is to create a database that creates the connection for you.
Then when you need to access the database, instantiate the database class and use the connection it provides.
I wouldn't be too worried about the resource cost of doing this. Its better than passing around the same single instantiated connnection object.

Sounds sensible. What I am a little worried about is not cost but the effective capability of the framework of getting rid of connection object when calling Dispose().

0

Thats very minimal concern. One connection object lingering until it gets garbage collected isn't going to be a problem. I don't have a very deep understanding of garbage collection (never really needed to dig into it) but the .Net version is apparently efficient.

0

Ok.

What I thought of is disposing of the connection like this :

conn.Dispose();
conn = null;

should not be harmful.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.