473,324 Members | 2,248 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,324 software developers and data experts.

Global Database connection in all classes

Hello,
I'm just starting to develop in asp.net and i have a question about
using a database connection globally in my app. I have set up the
procedures for getting all my connection string info which each page
will use, but my question relates to how to use the database
connection i create in all my classes.

I have a database class, in a separate namespace and file, i created
that handles all the connection opening, executing statements etc. I
also use 2 other classes in my asp.net app. which are in the same
namespace and same file.
1 holds information specific to the current user logged in, and the
other class is used as a global functions class. I want to be able to
open the db connection once, and no matter if i'm running a method in
the UserSession class, or the Global Functions class to be able to use
that same Database class i created, without creating a new instance of
the database class in each class i want to run this in.

example. my UserSession class might have the web login authentication
method in it that gets run when the user clicks the login button on
the login page, right after that method runs, which again is in the
user session class, i might run a method from the global functions
class that needs to use the database. i wanted to use the same
Database class i instantiated instead of creating a separate instance
of the database class for each class i needed it in. Can this be done?
or do i need to change my thinking around a little and use a different
model.

thanks for any help you can offer. Any links to any "best practice"
type info/articles would be appreciated as well.
Bryan
Nov 18 '05 #1
2 3475
I would suggest that you have a read of
http://msdn.microsoft.com/library/de...ml/daab-rm.asp

also have a read up of
http://msdn.microsoft.com/library/de...ml/daab-rm.asp

--

Regards,

Hermit Dave
(http://hdave.blogspot.com)
"Bryan" <br*********@yahoo.com> wrote in message
news:9e**************************@posting.google.c om...
Hello,
I'm just starting to develop in asp.net and i have a question about
using a database connection globally in my app. I have set up the
procedures for getting all my connection string info which each page
will use, but my question relates to how to use the database
connection i create in all my classes.

I have a database class, in a separate namespace and file, i created
that handles all the connection opening, executing statements etc. I
also use 2 other classes in my asp.net app. which are in the same
namespace and same file.
1 holds information specific to the current user logged in, and the
other class is used as a global functions class. I want to be able to
open the db connection once, and no matter if i'm running a method in
the UserSession class, or the Global Functions class to be able to use
that same Database class i created, without creating a new instance of
the database class in each class i want to run this in.

example. my UserSession class might have the web login authentication
method in it that gets run when the user clicks the login button on
the login page, right after that method runs, which again is in the
user session class, i might run a method from the global functions
class that needs to use the database. i wanted to use the same
Database class i instantiated instead of creating a separate instance
of the database class for each class i needed it in. Can this be done?
or do i need to change my thinking around a little and use a different
model.

thanks for any help you can offer. Any links to any "best practice"
type info/articles would be appreciated as well.
Bryan

Nov 18 '05 #2
Bryan,
From what I understand you are trying to do, best practice goes against it
;) You seem to be trying to set it up so that a single connection is opened
throughout the life of a request. With ADO.Net and connection pooling, the
prefered method is to keep connections closed for the shortest time possible
/ the smallest unit. It's far better to create new database connections and
open them on a as-needed basis.

For example, it's much better to do this:

function login_click
Call UserIsLoggedIn
if true then
Call GetUserInfo
end if
end funciton

function UserIsLoggedIn
open connection
validate user
close connection
end function
function GetUserInfo
open connection
get user info
close cnnection
end function
than to do this:

function login_click
open conection
Call UserIsLoggedIn
Call GetUserInfo
close connection
end funciton

function UserIsLoggedIn
validate user
end function
function GetUserInfo
get user info
end function

Granted that's a pretty trivial example...

If my word isn't good enough for you...From Scott Gu
(http://scottgu.com/PerformanceEurope.zip)

Code Recommendation:
"Open connections in your code late, and then close them early"
Don't hold on to connections for long periods of time - do not try to build
your own "smart" connection pool logic
Close the connection as soon as you are finished with it (this returns it to
the pool)
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Bryan" <br*********@yahoo.com> wrote in message
news:9e**************************@posting.google.c om...
Hello,
I'm just starting to develop in asp.net and i have a question about
using a database connection globally in my app. I have set up the
procedures for getting all my connection string info which each page
will use, but my question relates to how to use the database
connection i create in all my classes.

I have a database class, in a separate namespace and file, i created
that handles all the connection opening, executing statements etc. I
also use 2 other classes in my asp.net app. which are in the same
namespace and same file.
1 holds information specific to the current user logged in, and the
other class is used as a global functions class. I want to be able to
open the db connection once, and no matter if i'm running a method in
the UserSession class, or the Global Functions class to be able to use
that same Database class i created, without creating a new instance of
the database class in each class i want to run this in.

example. my UserSession class might have the web login authentication
method in it that gets run when the user clicks the login button on
the login page, right after that method runs, which again is in the
user session class, i might run a method from the global functions
class that needs to use the database. i wanted to use the same
Database class i instantiated instead of creating a separate instance
of the database class for each class i needed it in. Can this be done?
or do i need to change my thinking around a little and use a different
model.

thanks for any help you can offer. Any links to any "best practice"
type info/articles would be appreciated as well.
Bryan

Nov 18 '05 #3

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

Similar topics

2
by: Martin Lucas-Smith | last post by:
I have a class from within which other classes are called. In the constructor, I want to create an instance of a database connection, so that this database can be called elsewhere. <?php #...
3
by: mike.miller | last post by:
You could create a static class for the connection with static properties for the connection object itself and all of the associated properties. Just be aware that only one object can use a...
4
by: RD | last post by:
I opened a connection to the database in the login form. Now in another form I tried to insert values to a table. But the connection is not available in the form. How can I make the connection...
3
by: Faisal | last post by:
Hi. I'm in the process of moving an application from ASP to ASP.NET, & I'm writing in VB, using VS.NET. I'm new to the .NET framework & have a basic question regarding static objects defined in...
6
by: Prince | last post by:
I have a question about the global.asax.cs file. I'm reading info from a database to populate a DataGrid. I read somewhere that the opening of the database should occur in the global.asax.cs...
2
by: PRTC | last post by:
I'm trying to use the global.asax in my new web aplication proyect using the Application start to store my connection string GLOBAL.ASAX.vb Sub Application_Start(ByVal sender As Object,...
12
by: John M | last post by:
Hello, On Microsoft Visual Studio .NET 2003, I want to use some global elements, that can be used in each one of my pages. i.e I put a oleDBConnection on global.asax.vb How can I use it...
2
by: Lee | last post by:
I've been programming with Delphi for the past 4 years or so and while Delphi does allow globals, I use them very judiciously. I say that I *do* use them because I think that in some cases they...
35
by: Terry Jolly | last post by:
Web Solution Goal: Have a global database connection Why: (There will be 30+ tables, represented by 30+ classes) I only want to reference the database connection once. I put the connection...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.