Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Sharing database connection with controls

Reply
Thread Tools

Sharing database connection with controls

 
 
Mike P2
Guest
Posts: n/a
 
      06-03-2007
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

 
Reply With Quote
 
 
 
 
Eliyahu Goldin
Guest
Posts: n/a
 
      06-03-2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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
>



 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      06-03-2007
"Mike P2" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

 
Reply With Quote
 
Mike P2
Guest
Posts: n/a
 
      06-03-2007
On Jun 3, 3:46 am, "Eliyahu Goldin"
<(E-Mail Removed)> 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
> 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

 
Reply With Quote
 
Eliyahu Goldin
Guest
Posts: n/a
 
      06-03-2007
System.Data.SqlClient is fine.

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


"Mike P2" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> On Jun 3, 3:46 am, "Eliyahu Goldin"
> <(E-Mail Removed)> 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
>> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
Sharing a DBI::Mysql database connection with your children Andrew DeFaria Perl Misc 7 04-01-2008 11:01 PM
Sharing database connection from C to Python dgiagio@gmail.com Python 1 02-01-2006 09:16 PM
Database connection sharing Atif Jalal ASP .Net 1 08-08-2003 09:18 PM



Advertisments