Home All Groups Group Topic Archive Search About

Managing Database Connections!

Author
5 Sep 2006 9:11 PM
AJ
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

Author
5 Sep 2006 11:01 PM
Mark Rae
"AJ" <A*@discussions.microsoft.com> wrote in message
news:38D06517-14B0-47BF-A087-BF2A607F0D84@microsoft.com...

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

Bookmark and Share