473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 2098
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Robert Brewer | last post by:
Paul Moore wrote: > ...I *can* defend an argument that the proposal must > take a stance on the keyword proposed. The existence of people (me, > as an example :-)) whose vote would be changed by...
28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
1
by: Mehul Patel | last post by:
Our .Net team have been pondering about using keyword. We are using streams FileStream and BufferedStream. We use using keyword at FileStream, and not BufferedStream which wraps FileStream. So...
11
by: z_learning_tester | last post by:
Hello, yes another beginner question that I'm sure is obvious to many here :-) My book is so bad. Really. It uses the exact same example of code for using the new kw and for using virtual(in the...
7
by: Willem van Rumpt | last post by:
Hi all, coming from an unmanaged programming background, I took my time to sort out the IDisposable and finalizer patterns. Just when I thought I had it all conceptually neatly arranged, the...
3
by: Brian Gideon | last post by:
I stumbled across something odd today about the placement of the using keyword. Section 9.3.2 of the C# v1.1 specification did not answer my question. My confusion is isolated to what happens in...
3
by: flat_ross | last post by:
For anyone who is just getting into VB.NET and/or is starting to work with inheritance I would like to point out a potential pitfall. We found this confusion recently when code-reviewing an...
11
by: youngster94 | last post by:
Hey all, I've written a VB.Net app that creates picture badges complete with barcodes. The problem is that the barcode quality is not good enough to be read by scanners. I'm using the...
3
by: Igor | last post by:
Is there possibility to use IF conditions inside SELECT statements? For example, can i write something like this: CREATE PROCEDURE ( @OPTION int, @KEYWORD nvarchar(40) ) AS BEGIN
4
by: Pranjal9880 | last post by:
Hi all I am trying to parse the xml file using perl in which I am succeeded , I am able to fetch the data from the xml file by using one keyword. Now I want to do it using more than one keyword. It...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.