DataReader and DB Connection Closing

R

rockdale

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();
 
E

Eliyahu Goldin

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

rockdale

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 said:
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 said:
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();
 
Joined
Jun 27, 2011
Messages
1
Reaction score
0
public IDataReader DsFetch()
{
IDataReader Idr = null;
try
{

Database db = DatabaseFactory.CreateDatabase();
DbCommand dbcommand = db.GetSqlStringCommand("select * from RSSrnd");
dbcommand.CommandType = CommandType.Text;
return Idr =db.ExecuteReader(dbcommand);

}
catch (Exception ex)
{
throw ex;
}
finally
{

}
}
 
Joined
Jan 31, 2012
Messages
1
Reaction score
0
Hi Rockdale,

In fact the ExecuteReader method of the Database class, automatically closes its connection to the DB after the closing of the DataReader returned by the method.

This is because it uses CommandBehavior.CloseConnection.

Note the class source Database.cs Microsoft Practices.

I hope this helps.

Undo edits


Source Database.cs:



public virtual IDataReader ExecuteReader(DbCommand command)
{
DbConnection connection = OpenConnection();
PrepareCommand(command, connection);


try
{


return DoExecuteReader(command, CommandBehavior.CloseConnection);
}


catch
{
connection.Close();


throw;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top