ExecuteReader - CommandBehavior.CloseConnection

  • Thread starter Thread starter matt
  • Start date Start date
M

matt

hello,

when coding a datareader, i often use the following in my code:

conn.Open();
SqlDataReader dr =
command.ExecuteReader(CommandBehavior.CloseConnection);
...[do stuff w/ dr]...
dr.Close()

....with the understanding that, when the reader is closed so will the
connection.

but now im moving my code into a component's static functions. and i
starting wondering: "If i return the datareader to a client
application, will my client app's closure of the returned datareader
close this component's (static function) connection?

like so:

conn.Open();
SqlDataReader dr =
command.ExecuteReader(CommandBehavior.CloseConnection);

if (dr.HasRows)
{
return dr;
}
else
{
return null;
}


....anyone know for a fact?


thanks!
matt
 
Well, for this reason, you shouldn't be returning a DataReader to the client
in the first place. If you want to get data to pass to a client, use a
DataAdapter with a SELECT SQL statement/SP and return a DataTable to the
client instead.
 
You can also look for alternative solution, if you don't want to rely on
client closing the connection (answer itself is yes as Miha pointed out)

Using Delegates With Data Readers to Control DAL Responsibility
http://aspalliance.com/526
 
Back
Top