473,395 Members | 1,474 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,395 software developers and data experts.

Open/Close Database Connection for each page

Hello,

In my past ASP pages, at the top I used an include file to open an ado
connection for the entire page, then at the bottom, I would have another
include file to close the connection.

Now that I'm moving to .NET, and using ado.net as well, does anyone have any
ideas to accomplish this for asp.net. I'd like for it to be as much behind
the scenes as possible, hopefully so I'll never have to look at statements
like myConnection.open and myConnection.close in my main code.

Thanks,
--Michael
Nov 18 '05 #1
2 1871
Michael,

ASP was nice but it was never object oriented... plus since it was
interpreted... it could be tuned but only to a certain extent.
With ASP.NET... its fully object oriented and you go about fetching the data
as you do in a proper n-tier application. (Though nothing stops you from
doing otherwise)
This is my recommended approach:
Say you have customer related pages where you do whole lot of different
things but still pertaining to the User.
well you create a UserDB class... And create methods which will either
return or take say an instance of a UserDetail Class.

so all you have to do in your aspx code behind file is create instance of
UserDB class and call corresponding methods. It is much more object oriented
and much easier to debug...

Unfortunately in asp.net you dont have the #include which used to be so
frequently used... so you have to open the connection and close it in the
same file...

class UserDetail
{
public int UserID;
public string Name;
public string Email;
}

class UserDB
{
public UserDetail GetUserDetail(UserID)
{
SqlConnection myCon = SqlConnection("connection string here")
try
{
SqlCommand myCommand = new SqlCommand("sp_Users_Select", myCon);
myCommand.CommandType = CommandType.StoredProc;

SqlParameter parameterUserID = SqlParameter("@UserID",
SqlDbType.Int, 4);
parameterUserID.value = UserID;
myCommand.Parameters.Add(parameterUserID);

SqlParameter parameterUserName = SqlParameter("@UserName",
SqlDbType.NVarChar, 50);
parameterUserName.Direction = ParameterDirection.Output;
myCommand.Parameters.Add(parameterUserName);

SqlParameter parameterUserID = SqlParameter("@UserEmail",
SqlDbType.NVarChar, 50);
parameterUserEmail.Direction = ParameterDirection.Output;
myCommand.Parameters.Add(parameterUserEmail);

myCon.open();
myCommand.ExecuteNonQuery();
UserDetail myUser = new UserDetail();
myUser.UserID = UserID;
myUser.UserName = parameterUserName.value;
myUser.UserEmail = parameterUserEmail.value;
myCommand.Dispose();
return myUser;
}
finally
{
myCon.Close();
myCon.Dispose();
}
}
}

hope this example helps...

--
Regards,

HD

"Michael" <raterus@localhost> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hello,

In my past ASP pages, at the top I used an include file to open an ado
connection for the entire page, then at the bottom, I would have another
include file to close the connection.

Now that I'm moving to .NET, and using ado.net as well, does anyone have any ideas to accomplish this for asp.net. I'd like for it to be as much behind the scenes as possible, hopefully so I'll never have to look at statements
like myConnection.open and myConnection.close in my main code.

Thanks,
--Michael

Nov 18 '05 #2
Trying to think like ASP when working with ASP.Net is going to drive you
crazy. ADO.Net and ADO have very little in common, about as much as ASP and
ASP.Net have in common.

ASP.Net is object-oriented, which means that you have to think in objects
and classes. For example, typically, an ASP.Net application that talks to a
database includes some kind of class or classes that do the database work,
and return results. This is the "N-Tier" approach, which puts all database
functionality into a tier of its own, and separates it from code that works
with the Interface tier. This makes code maintenance much simpler, as well
as efficient, and would be the way I would recommend you design your
application.

Also, keep in mind that ASP.Net uses Connection Pooling, so you don't have
to worry about using the same Connection for multiple database operations
(which can be tricky and difficult at best with ADO.Net, for several
reasons). Instead, the recommended practice is to open and close your
Connections explicitly whenever you need to connect to the database. As long
as the Connections all use the same Connection String, they will be pooled,
and you wan't actually be creating a new one each time you "create" one;
instead, you'll be using one you already created previously, which was
pooled.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Michael" <raterus@localhost> wrote in message
news:#9**************@tk2msftngp13.phx.gbl...
Hello,

In my past ASP pages, at the top I used an include file to open an ado
connection for the entire page, then at the bottom, I would have another
include file to close the connection.

Now that I'm moving to .NET, and using ado.net as well, does anyone have any ideas to accomplish this for asp.net. I'd like for it to be as much behind the scenes as possible, hopefully so I'll never have to look at statements
like myConnection.open and myConnection.close in my main code.

Thanks,
--Michael

Nov 18 '05 #3

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

Similar topics

4
by: Andras Gilicz | last post by:
Hi VB fans I'm working on a relatively large project in VB6 with about a dozen forms, including graphs, labels, text boxes, etc. The software itself is actually a flow simulator with more or...
4
by: Macca | last post by:
Hi, I have an windows forms application that accesses a SQL database I have a few questions as to connecting to the database. This application will run 24 hours a day. It is a monitoring...
5
by: Saulot | last post by:
Hi, Facing a big problem. In my Default.aspx page, I open a connection with ma Sql Server DataBase through my objects framework (A "SetDefaultConnectionString" property which open my...
6
by: B B | last post by:
Okay, here is what's happening: I have a reasonably fast laptop (1.4 GHz Mobile M, so comparable to 2.5GHz P4) doing .net development. Running Windows XP pro, SP2 IIS is installed and running...
4
by: mescano | last post by:
I am currently implementing a singleton pattern for accessing a database. Is it advisable to close the connection to the database at all -- thus leaving it open or should it be closed. If closed,...
6
by: ste | last post by:
Hi there, I'm just beginning to learn PHP and MySQL, but I'm finding it difficult! I wondered if someone could help me out with a problem I'm having, or at least point me in the right...
8
by: Imran Aziz | last post by:
Hello All, Like in C++ I tried to use constructor to open a database connection and distructor to close the database connection, it now turns out that one cannot create distrutors in C# classes. ...
6
by: Mike | last post by:
We are intermitantly receiving this error on our website. ExecuteReader requires an open and available Connection. The connection's current state is connecting. Following is the code from the Load...
2
by: Tony Johansson | last post by:
Hello! How does this connection pool function actually ? As I have been told is that when the connection is returned to the pool the connection to the database is still open. What you do is that...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.