Managing Database Connections!

G

Guest

Hi all,

Just throwing something out to the newsgroup to get a feel for what others
are doing.

I am noticing that with my current coding practice, every time i want to
update a database i generally implement the following pattern.

1) Open db connnection
2) Execute Query
3) Close Db Connection

Having written numerous events / methods that require db access, i am
getting tired of having to manage the Db connection in each routine; ie
create and close a connection like so.

//create db connection
SqlConnection Cn = new
SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);

//open db connection
Cn.Open();

//close db connection
Cn.Close();

Apart from laziness, i have realized when a post back requires a number of
actions I am opening & closing the database numerous times, and wonder how
efficient
it is.

How does everyone else handle their db work.

Does anyone else have a method that allows you to reuse an existing Db
Connection? A singleton class ect???

I am about to add a DAL to an existing 1.1 project....how would ya'll manage
your db connections in their.

Would love any sample code and / or tutorials ?

Input appreciated.

Adam
 
M

Mark Rae

How does everyone else handle their db work.
DAL.

Does anyone else have a method that allows you to reuse an existing Db
Connection?

That's what connection pooling is for. You should always close your database
connection object as soon as possible - so long as the connection string
which was used to open the connection is the same, ADO.NET will reuse it
from the connection pool for you by default...
I am about to add a DAL to an existing 1.1 project....how would ya'll
manage
your db connections in their.

Would love any sample code and / or tutorials ?

Email me privately and I'll happily send you my v1.1 DAL for SQL Server,
together with a class which simulates nullable datatypes - not necessary for
v2...
 

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,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top