Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Open/Close Database Connection for each page

Reply
Thread Tools

Open/Close Database Connection for each page

 
 
Michael
Guest
Posts: n/a
 
      12-30-2003
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


 
Reply With Quote
 
 
 
 
Hermit Dave
Guest
Posts: n/a
 
      12-30-2003
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:%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      12-30-2003
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:#(E-Mail Removed)...
> 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
>
>



 
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
how do i? Full scan of each control in each grid row cell John Blair ASP .Net 1 08-03-2005 11:02 AM
xsl:for-each for each 3 elements problem Tjerk Wolterink XML 3 11-03-2004 05:22 PM
TCP socket program where each node initiates connection with some nodes and accepts connection with the others Kathryn Bean C++ 1 10-28-2004 12:33 PM



Advertisments