Antimon,
You should create your connection when you need it, and dispose of it
when you are done with your operation, no more, no less.
In an ASP.NET application, I can't see why you would need to maintain an
open connection beyond the processing of your page (in other words, on the
class level, and not disposing of it).
My recommendation would be to have a static method which will create
your connection for you, unopened (it will set up the connection string, so
you can change the details of your connection everywhere when needed later
on). You can store this on the page level, getting it on the Load event of
the page.
You would have to make sure that you close it in the Unload event on
your page as well.
This way, you can share the database connection throughout the methods
on your page. If you want your classes to use the same database connection,
you will have to pass it to them somehow.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"Antimon" <an*****@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Hi,
I'am working on an asp.net web project. was looking at some sources and
as i can see ppl creates new "sqlconnection" instances for each query
(or for every module). I know there's a pooling mechanism but isn't it
better to have a connection created at "page_load" and closed when
request handling completes?
Shall i create a new connection for every single module or create just
one for every page request?
Thanks.