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

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.

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().

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.

Ok.

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

conn.Dispose();
conn = null;

should not be harmful.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.