467,151 Members | 1,098 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,151 developers. It's quick & easy.

Sharing database connection with controls

Hi. I'm writing controls that have to query the database, and it
bothers me that I might have several of these controls on a page that
each create, open, and close their own connection with the same
connection string, and the page class has a connection object too.

I'm using the code-behind way of doing things, and I considered
leaving the database connection open as a field in the page's class
and somehow letting the controls use that same connection, but then I
want to be sure that every page does have a database connection
object.

What is the best way of sharing the database connection object between
the page's class and the controls' classes? Can I create a connection
object on the page automatically some way in global.asax?

Thanks,
Mike PII

Jun 3 '07 #1
  • viewed: 1426
Share:
4 Replies
If you use exactly the same connection string, asp.net (rather ado.net for
this matter) will automatically share the same connection between all your
objects. This is called "connection pooling". The best practice is to put
the code to open connection just before the database access operation and to
close it as soon as you finish the operation. Ado.net will take care about
actual opening and closing connections. Trying to keep connection open will
result in degrading performance.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Mike P2" <su***********@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
Hi. I'm writing controls that have to query the database, and it
bothers me that I might have several of these controls on a page that
each create, open, and close their own connection with the same
connection string, and the page class has a connection object too.

I'm using the code-behind way of doing things, and I considered
leaving the database connection open as a field in the page's class
and somehow letting the controls use that same connection, but then I
want to be sure that every page does have a database connection
object.

What is the best way of sharing the database connection object between
the page's class and the controls' classes? Can I create a connection
object on the page automatically some way in global.asax?

Thanks,
Mike PII

Jun 3 '07 #2
"Mike P2" <su***********@gmail.comwrote in message
news:11**********************@g4g2000hsf.googlegro ups.com...
I considered leaving the database connection
As Eliyahu mentions, this is one of the worst things you can do in terms of
performance and scalability of an ASP.NET app...
--
http://www.markrae.net

Jun 3 '07 #3
On Jun 3, 3:46 am, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.orgwrote:
If you use exactly the same connection string, asp.net (rather ado.net for
this matter) will automatically share the same connection between all your
objects. This is called "connection pooling". The best practice is to put
the code to open connection just before the database access operation and to
close it as soon as you finish the operation. Ado.net will take care about
actual opening and closing connections. Trying to keep connection open will
result in degrading performance.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net
Cool. So is the stuff in the System.Data.SqlClient Ado.net? Or do I
have to use those OLE classes?

Thanks for your replies,
Mike PII

Jun 3 '07 #4
System.Data.SqlClient is fine.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
"Mike P2" <su***********@gmail.comwrote in message
news:11**********************@p47g2000hsd.googlegr oups.com...
On Jun 3, 3:46 am, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.orgwrote:
>If you use exactly the same connection string, asp.net (rather ado.net
for
this matter) will automatically share the same connection between all
your
objects. This is called "connection pooling". The best practice is to put
the code to open connection just before the database access operation and
to
close it as soon as you finish the operation. Ado.net will take care
about
actual opening and closing connections. Trying to keep connection open
will
result in degrading performance.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP
[ASP.NET]http://msmvps.com/blogs/egoldinhttp://usableasp.net

Cool. So is the stuff in the System.Data.SqlClient Ado.net? Or do I
have to use those OLE classes?

Thanks for your replies,
Mike PII

Jun 3 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Pam Ammond | last post: by
1 post views Thread by Atif Jalal | last post: by
3 posts views Thread by grooby | last post: by
1 post views Thread by dgiagio@gmail.com | last post: by
5 posts views Thread by Usman Jamil | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.