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

DataReader and DB Connection Closing

I am using Microsoft.Practices.Enterprise.Library

When I use IDataReader I saw this line
"It is the responsibility of the caller to close the connection and
reader when finished."

I was wondering if I close the DataReader do I automatic ally close the
Connection? If not , how can I close the Connection outside the
ExecuteReader function?

I code snippet is like following:

------------------------------------------------
IDataReader oDr =null;

Database db = DatabaseFactory.CreateDatabase();

string sqlCommand = "my_stored_proc";
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
oDr = db.ExecuteReader(dbCommand);
/*DO SOMETHING WITH oDR*/
/*I close the DataReader here, do I need to close connection also?*/
oDr.Close();

-------------------------------------------------
Thanks

-rockdale

Sep 21 '06 #1
3 2764
Yes, I would also recommend explicit disposing connection objects. It's a
got idea to put it in a finally block.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"rockdale" <ro************@gmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
>I am using Microsoft.Practices.Enterprise.Library

When I use IDataReader I saw this line
"It is the responsibility of the caller to close the connection and
reader when finished."

I was wondering if I close the DataReader do I automatic ally close the
Connection? If not , how can I close the Connection outside the
ExecuteReader function?

I code snippet is like following:

------------------------------------------------
IDataReader oDr =null;

Database db = DatabaseFactory.CreateDatabase();

string sqlCommand = "my_stored_proc";
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
oDr = db.ExecuteReader(dbCommand);
/*DO SOMETHING WITH oDR*/
/*I close the DataReader here, do I need to close connection also?*/
oDr.Close();

-------------------------------------------------
Thanks

-rockdale

Sep 21 '06 #2
Rockdale,

Here is some info on closing datareaders.

I always wrap my code in a try / finally block and put the close into the
finally so that even if an error is encountered the reader and connection
are still closed.

http://msdn2.microsoft.com/en-us/library/haa3afyz.aspx

Regards,

--
S. Justin Gengo
Web Developer / Programmer

Free code library:
http://www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"rockdale" <ro************@gmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
>I am using Microsoft.Practices.Enterprise.Library

When I use IDataReader I saw this line
"It is the responsibility of the caller to close the connection and
reader when finished."

I was wondering if I close the DataReader do I automatic ally close the
Connection? If not , how can I close the Connection outside the
ExecuteReader function?

I code snippet is like following:

------------------------------------------------
IDataReader oDr =null;

Database db = DatabaseFactory.CreateDatabase();

string sqlCommand = "my_stored_proc";
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
oDr = db.ExecuteReader(dbCommand);
/*DO SOMETHING WITH oDR*/
/*I close the DataReader here, do I need to close connection also?*/
oDr.Close();

-------------------------------------------------
Thanks

-rockdale

Sep 21 '06 #3
Hi, Eliyahu

Thanks for the reply.
But how can I dispose the connection object as the db connection is
created and opened in the database factory

I know I must close the connection if I create and open this connection
but here as I am using the Data Access block, how do I explicitly close
the connection and dispose it? Or should I use dataset since "The
connection is closed by the ExecuteDataSet function" ? (comment from
Microsoft Enterprise Library)

Thanks again
-phelix

Eliyahu Goldin wrote:
Yes, I would also recommend explicit disposing connection objects. It's a
got idea to put it in a finally block.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"rockdale" <ro************@gmail.comwrote in message
news:11*********************@k70g2000cwa.googlegro ups.com...
I am using Microsoft.Practices.Enterprise.Library

When I use IDataReader I saw this line
"It is the responsibility of the caller to close the connection and
reader when finished."

I was wondering if I close the DataReader do I automatic ally close the
Connection? If not , how can I close the Connection outside the
ExecuteReader function?

I code snippet is like following:

------------------------------------------------
IDataReader oDr =null;

Database db = DatabaseFactory.CreateDatabase();

string sqlCommand = "my_stored_proc";
DbCommand dbCommand = db.GetStoredProcCommand(sqlCommand);
oDr = db.ExecuteReader(dbCommand);
/*DO SOMETHING WITH oDR*/
/*I close the DataReader here, do I need to close connection also?*/
oDr.Close();

-------------------------------------------------
Thanks

-rockdale
Sep 21 '06 #4

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

Similar topics

3
by: Craig | last post by:
I have some methods that open a database connection, get some data and then return a datareader. How do I manage closing the connection to the database then? public OracleDataReader...
5
by: Joel Reinford | last post by:
I am attempting to verify whether a datareader is closed by calling the databind on a control. for example (fake code) Dim rdr as SqlDataReader rdr = mycommand.executeReader... ...
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
2
by: Andrei Pociu | last post by:
In a typical ASP .NET Web Application (website), I'm currently using a class where I declare some public static objects. For example there's the place where I initialize the SqlConnection. Also...
3
by: Paolo Pignatelli | last post by:
I have a Function in a Class File that uses ApplicationBlocks: Public Function GetProductsByCategory(ByVal CategoryID As Integer) Dim myConnection As SqlConnection = New...
3
by: Lubomir | last post by:
Hi, I have a class A with a public method GetReader: public OleDbDataReader GetReader() { SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings); SqlCommand...
7
by: Arsalan | last post by:
I have a function which return datareader Public Shared Function ReturnDReader(ByVal query As String) As OleDbDataReader Dim Connection_String As String =...
17
by: Alan Silver | last post by:
Hello, I have a generic method in a utility class that grabs an sqldatareader and returns it. Due to the fact that (AFAIK), you can't close the database connection before you've read the data,...
7
by: Diffident | last post by:
Hello All, I would like to use DataReader based accessing in my Data Access Layer (DAL). What is considered to be a best practice while returning from a DAL method that executes a query and...
20
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: 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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.