Using keyword

  • Thread starter Thread starter INeedADip
  • Start date Start date
I

INeedADip

Can anyone here confirm for sure that reader.Close() and Dispose() will
get called using the following routine:

using(SqlDataReader reader = GetOpenReaderFunction())
{
while(reader.Read()) doSomething(reader);
}

I have seen examples here and there with mixed assumptions.
 
INeedADip,

Yes, it will definitely close. For all intents and purposes, the C#
compiler translates that into:

SqlDataReader reader = GetOpenReaderFunction();

try
{
while(reader.Read()) doSomething(reader);
}
finally
{
if (reader != null)
{
((IDispose) reader).Dispose();
}
}

Hope this helps.
 
Dispose will be called automatically when the scope under the using
statement exits.

And Dispose will call Close().
 
INeedADip said:
Can anyone here confirm for sure that reader.Close() and Dispose() will
get called using the following routine:

using(SqlDataReader reader = GetOpenReaderFunction())
{
while(reader.Read()) doSomething(reader);
}

I have seen examples here and there with mixed assumptions.

Dispose will be called when the using block is exited, whether through
normal code execution or if an exception occurs. The Dispose method of
SqlDataReader calls Close to insure that the connection has been closed.

As a generic statement, using insures that Dispose will be called. It
is up to the implementation of Dispose that insures resources are
properly released.
 
Hi,

INeedADip said:
Can anyone here confirm for sure that reader.Close() and Dispose() will
get called using the following routine:

using(SqlDataReader reader = GetOpenReaderFunction())
{
while(reader.Read()) doSomething(reader);
}

I have seen examples here and there with mixed assumptions.

To be exact it's Dispose which is called and in this particular case Dispose
does call Close.

Note that you still need to close the connection, as the closing of the
DataReader does not close it.
UNLESS you created your reader using SqlCommand.ExecuteReader(
CommandBehavior.CloseConnection );
 
Hi Tom,


Dispose will be called when the using block is exited, whether through
normal code execution or if an exception occurs. The Dispose method of
SqlDataReader calls Close to insure that the connection has been closed.

It's the Close of the datareader or the one of the connection the one that
is called?
I was under the impression it was the Reader, as this is needed to have
access to stuff like, output parameters, records count. and return values.

The connection is only closed when the correct CommandBehaviour is passed in
the ExecuteReader

Correct me if I'm wrong
 
INeedADip said:
Can anyone here confirm for sure that reader.Close() and Dispose() will
get called using the following routine:

using(SqlDataReader reader = GetOpenReaderFunction())
{
while(reader.Read()) doSomething(reader);
}

I have seen examples here and there with mixed assumptions.

Close() won't be called, but Dispose() will be (which is enough).

The only problem would be if GetOpenReaderFunction() acquired a
SqlDataReader but then threw an exception before returning it.
 
Close() won't be called, but Dispose() will be (which is enough). <
Well, Close will be called by Dispose. (at least according to the
disassembled code for SqlDataReader).
But you probably mean that the using clause calls dispose directly, not
close.
 
John Wood said:
Well, Close will be called by Dispose. (at least according to the
disassembled code for SqlDataReader).
But you probably mean that the using clause calls dispose directly, not
close.

Exactly :)
 
Hi,


John Wood said:
Well, Close will be called by Dispose. (at least according to the
disassembled code for SqlDataReader).
But you probably mean that the using clause calls dispose directly, not
close.

A bad thing is this is not clearly stated in the documentation. I did not
check msdn2 though.
 

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

Back
Top