ExecuteReader - CommandBehavior.CloseConnection

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
 
S

Scott M.

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.
 
T

Teemu Keiski

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
 

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

Top