Proper Way to work with SQL Connections

B

Brandon Potter

Hi,

Basic SQL question.

I have your typical dynamic-content, about-10-SQL-requests-per-page web
site. However, I have run into some problems in the way that I'm doing
things in that sometimes the connection just fails, closes for no apparent
reason, or there's an open SqlDataReader somewhere, even though the code
does close every SqlDataReader when reading is finished.

I have created a SqlConnection object and opened the connection when the
user's session is created. For all the SQL requests that the pages make,
they just execute SqlCommands and SqlReaders using the one instance of
SqlConnection. When the session is done, I close the connection.

Should I be using the one instance of SqlConnection (my reasoning being that
it would be more efficient to hold the connection open rather than have the
extra load of opening and closing the connection), or should I be opening
and closing the connection for each SQL request? Or somewhere in between?

Thanks,
Brandon
 
K

Kevin Spencer

You should open and close your Connections as quickly as possible.
Connection Pooling will handle the performance for you. Most likely this is
the cause of your problems. An additional note: Leverage the various caching
mechanisms afforded to you to minimize the number of hits to the database.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top