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

Is this a good way to close DB Connection in ASP.NET?

Bob
Is this a good way to close DB Connection in ASP.NET?

I've been trying to come up a way to close DB connection automatically (or
sort of) in my ASP.NET application without having to write the close()
method everywhere. Every web page in my application needs to call the
database, and all DB access and SQL Command code are wrapped in a separate
class (called DBAccess below). I don't want to open and close the
connection within every public method of the DBAccess class as some of the
pages need to call several methods. My goal is to make one page instance
create one instance of the DBAccess and use only one connection, and no need
for page developer to explicitly open and close the underlying connection
(as it should be wrapped inside DBAccess). Here's what I came up with and
would like to get some comments on whether this is a good approach.

---------------------------------------------------------------
/************************************************** *****
The DBAccess class implements IDisposable so the public Dispose
method can be called to close the connection.
************************************************** ******/
public class DBAccess : IDisposable {
private System.Data.SqlClient.SqlConnection _conn;

public DBAccess() {
//Initialize _conn and call _conn.Open()
}

public System.Data.DataTable SomeDBCall() {
//Call a Stored Proc to return a DataTable
}

public void AnotherCall() {
//Call a Stored Proc to update a table
}

//more methods.........

public void Dispose() {
_conn.Close();
}
}

/************************************************** *******
A sample web page class. The using statement guarantees the
DBAccess.Dispose() would be called. Alternatively, the Page
class' Dispose method can be overridden to call DBAccess.Dispose().
Probably more flexible to override Page.Dispose
************************************************** *******/
public class MyWebPage : System.Web.UI.Page {
private DBAccess _db = new DBAccess();

private void Page_Load(object sender, System.EventArgs e) {
//Do something....
using (_db) {
//execute calls to the DB;
}
//Do some other thing.....
}
}

Nov 18 '05 #1
1 1443
yes that will work

--
Regards,
Alvin Bruney
Got tidbits? Get it here...
http://tinyurl.com/3he3b
" Bob" <bo*******@yahoo.com> wrote in message
news:uT**************@TK2MSFTNGP11.phx.gbl...
Is this a good way to close DB Connection in ASP.NET?

I've been trying to come up a way to close DB connection automatically (or
sort of) in my ASP.NET application without having to write the close()
method everywhere. Every web page in my application needs to call the
database, and all DB access and SQL Command code are wrapped in a separate
class (called DBAccess below). I don't want to open and close the
connection within every public method of the DBAccess class as some of the
pages need to call several methods. My goal is to make one page instance
create one instance of the DBAccess and use only one connection, and no need for page developer to explicitly open and close the underlying connection
(as it should be wrapped inside DBAccess). Here's what I came up with and
would like to get some comments on whether this is a good approach.

---------------------------------------------------------------
/************************************************** *****
The DBAccess class implements IDisposable so the public Dispose
method can be called to close the connection.
************************************************** ******/
public class DBAccess : IDisposable {
private System.Data.SqlClient.SqlConnection _conn;

public DBAccess() {
//Initialize _conn and call _conn.Open()
}

public System.Data.DataTable SomeDBCall() {
//Call a Stored Proc to return a DataTable
}

public void AnotherCall() {
//Call a Stored Proc to update a table
}

//more methods.........

public void Dispose() {
_conn.Close();
}
}

/************************************************** *******
A sample web page class. The using statement guarantees the
DBAccess.Dispose() would be called. Alternatively, the Page
class' Dispose method can be overridden to call DBAccess.Dispose().
Probably more flexible to override Page.Dispose
************************************************** *******/
public class MyWebPage : System.Web.UI.Page {
private DBAccess _db = new DBAccess();

private void Page_Load(object sender, System.EventArgs e) {
//Do something....
using (_db) {
//execute calls to the DB;
}
//Do some other thing.....
}
}

Nov 18 '05 #2

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

Similar topics

3
by: sam | last post by:
Hello group, I have a function which is used to initiate sqlDataReader object. I was trying to invoke the close method on the DataReader object but cant really do that as the function returns a...
3
by: Daniel | last post by:
TcpClient close() method socket leak when i use TcpClient to open a connection, send data and close the TcpClient with myTcpClientInstance.Close(); it takes 60 seconds for the actual socket on...
2
by: InvisibleMan | last post by:
Hi, I feel a little dumb for asking this (considering im writing TSQL) but there doesn't seem to be any definitive answers on the search engines... Okay I understand that if you open the ADO...
3
by: Andyza | last post by:
In my Global.asa file I have the following 2 subs for opening and closing my db connection: Sub OpenConn() Dim conn Set conn = Server.CreateObject("ADODB.Connection") conn.Open...
5
by: Mojtaba Faridzad | last post by:
Hi, (newbie in C#) is it a good idea to use SQL Connection, SQL Data Adapter,... in C#? when I am design a form, I can use SQL Connection and set the properties to connect to a database. I guess...
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
1
by: Chris | last post by:
Hi, We have an application where users must fill the same form several times, creating each time a record in the database. When checking the checkbox in this form means the last record. ...
6
by: S_K | last post by:
Hi all! I'm writing an ASP.NET web application that uses an Oracle database. I OPEN the Oracle connection by using the following code: if (this.ConnectionString != "") { this.Connection =...
4
by: kessa | last post by:
Hi Guys, I've recently had a problem with my site displaying a "system resources exceeded" error message ... and whilst searching this site for a solution (which I think I've now found) I came...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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...
1
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...
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)...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
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.