Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

Sharing database connection with controls

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mike P2 - 03 Jun 2007 04:02 GMT
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
Eliyahu Goldin - 03 Jun 2007 08:46 GMT
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.

Signature

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

> 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
[quoted text clipped - 13 lines]
> Thanks,
> Mike PII
Mike P2 - 03 Jun 2007 15:19 GMT
On Jun 3, 3:46 am, "Eliyahu Goldin"
<REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
> 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
[quoted text clipped - 8 lines]
> 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
Eliyahu Goldin - 03 Jun 2007 15:29 GMT
System.Data.SqlClient is fine.

Signature

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

> On Jun 3, 3:46 am, "Eliyahu Goldin"
> <REMOVEALLCAPITALSeEgGoldD...@mMvVpPsS.org> wrote:
[quoted text clipped - 22 lines]
> Thanks for your replies,
> Mike PII
Mark Rae - 03 Jun 2007 09:51 GMT
> 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...

Signature

http://www.markrae.net


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.