By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,544 Members | 2,021 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,544 IT Pros & Developers. It's quick & easy.

DataReader and DB Connection Closing

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.