469,076 Members | 1,364 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,076 developers. It's quick & easy.

Using keyword

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.

Jan 5 '06 #1
10 1926
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"INeedADip" <in*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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.

Jan 5 '06 #2
Dispose will be called automatically when the scope under the using
statement exits.

And Dispose will call Close().

"INeedADip" <in*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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.

Jan 5 '06 #3
INeedADip wrote:
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.
--
Tom Porterfield
Jan 5 '06 #4
Hi,

"INeedADip" <in*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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 );

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 5 '06 #5
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

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 5 '06 #6
INeedADip <in*******@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 5 '06 #7
> 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.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
INeedADip <in*******@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 5 '06 #8
John Wood <john.wood@priorganize_nospam__.com> wrote:
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.


Exactly :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 5 '06 #9
Ignacio Machin ( .NET/ C# MVP ) wrote:
Correct me if I'm wrong


You are correct, it is the reader that is closed, not the connection.
Thanks.
--
Tom Porterfield
Jan 5 '06 #10
Hi,
"John Wood" <john.wood@priorganize_nospam__.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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.


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

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 6 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Robert Brewer | last post: by
28 posts views Thread by Daniel | last post: by
1 post views Thread by Mehul Patel | last post: by
11 posts views Thread by z_learning_tester | last post: by
7 posts views Thread by Willem van Rumpt | last post: by
3 posts views Thread by Brian Gideon | last post: by
3 posts views Thread by Igor | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.