473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SQLConnection asp.net Best Practices?

mid
Hello!,

What is the Best Practice about dealing with a sqlconnection while page
executes?
Let me explain:
Supose you have several dropdownlists, datagrids, etc, in the same page,
that need to retreive data from sql server. Right now I open/close a sql
connection for each object that I want to populate, but I think this is not
the good way...

If I open the connection at page.load and close at page.unload and
use/reuse that connection in all the subs that get executed while page...
executes :), is this a good idea??

How do you gurus deal with this question?

Thanks
mid
Nov 19 '05 #1
4 4463
Connection pooling takes care of connection reuse and the like. Open a
connection at the last possible second and close it as soon as possible
afterwards. Don't open at the beginning and close at the end.
Nov 19 '05 #2
Ideally, you shouldn't be using any database logic in your Page class.
Instead, build a business class that does your database operations, and have
that class do the work. The Page should only contain interface logic that
uses the business class(es) to manipulate data, and displays the data in the
browser interface. The business class should open and close Connections as
quickly as possible.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Neither a follower
nor a lender be.

"mid" <mi*********@midbarnospam.org> wrote in message
news:1f****************@midbarnospam.org...
Hello!,

What is the Best Practice about dealing with a sqlconnection while page
executes?
Let me explain:
Supose you have several dropdownlists, datagrids, etc, in the same page,
that need to retreive data from sql server. Right now I open/close a sql
connection for each object that I want to populate, but I think this is not the good way...

If I open the connection at page.load and close at page.unload and
use/reuse that connection in all the subs that get executed while page...
executes :), is this a good idea??

How do you gurus deal with this question?

Thanks
mid

Nov 19 '05 #3
mid
Thanks Scott,

I am aware of connection pooling but thought it was helpfull when several
users are executing the page (in different threads). Do you mean that when
I open a connection to populate a dropdown then close it then open it again
to populate another object there is no overhead? Behind the scenes the
connection is still open?

Where to declare the connection (not open)? At page Load?

Thanks
mid

On Mon, 13 Dec 2004 12:01:01 -0800, Scott Simons wrote:
Connection pooling takes care of connection reuse and the like. Open a
connection at the last possible second and close it as soon as possible
afterwards. Don't open at the beginning and close at the end.

Nov 19 '05 #4
I'd keep the connection as a local variable inside of a method. If
down the road you decide to use some common code as a data access
layer this approach keeps things simple to convert.

--
Scott
http://www.OdeToCode.com/blogs/scott/

On Tue, 14 Dec 2004 09:33:07 +0000, mid <mi*********@midbarnospam.org>
wrote:
Thanks Scott,

I am aware of connection pooling but thought it was helpfull when several
users are executing the page (in different threads). Do you mean that when
I open a connection to populate a dropdown then close it then open it again
to populate another object there is no overhead? Behind the scenes the
connection is still open?

Where to declare the connection (not open)? At page Load?

Thanks
mid

On Mon, 13 Dec 2004 12:01:01 -0800, Scott Simons wrote:
Connection pooling takes care of connection reuse and the like. Open a
connection at the last possible second and close it as soon as possible
afterwards. Don't open at the beginning and close at the end.


Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: byrocat | last post by:
I'm chasing after a documetn that was available on one of the Microsoft websites that was titled somethign like "MS SQL Server Best Practices" and detailed a nyumber of best practices about...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
17
by: LP | last post by:
Hello, Here's the scenario: Object A opens a Sql Db connection to execute number of SqlCommands. Then it needs to pass this connection to a constructor of object B which in turn executes more...
13
by: john doe | last post by:
A quick question, about so-called 'best practices', I'm interested in which of A/B of the two examples people would choose, and why. public enum MyEnum { Option1 = 0, Option2 = 1, Option3 =...
2
by: Amelyan | last post by:
Could anyone recommend a book (or a web site) that defines best practices in ASP.NET application development? E.g. 1) Precede your control id's with type of control btnSubmit, txtName, etc. 2)...
10
by: jojobar | last post by:
Hello, I am trying to use vs.net 2005 to migrate a project originally in vs.net 2003. I started with creation of a "web site", and then created folders for each component of the site. I read...
10
by: Ren | last post by:
Hi All, I'm still rather new at vb.net and would like to know the proper way to access private varibables in a class. Do I access the variable directly or do I use the public property? ...
3
by: John Dalberg | last post by:
I am looking for an ASP.NET application on CodePlex which exemplifies best practices for the following: - Use of interfaces - Seperation of the UI, business and data tiers - Data Tier that uses...
4
by: Victor | last post by:
Hi Guys I have a problem here. I want to improve the performance for a website. When I looked into the system, I have found that the system made the "SqlConnection Object" static. That mean only...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.