473,776 Members | 1,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using keyword

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

using(SqlDataRe ader reader = GetOpenReaderFu nction())
{
while(reader.Re ad()) doSomething(rea der);
}

I have seen examples here and there with mixed assumptions.

Jan 5 '06 #1
10 2131
INeedADip,

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

SqlDataReader reader = GetOpenReaderFu nction();

try
{
while(reader.Re ad()) doSomething(rea der);
}
finally
{
if (reader != null)
{
((IDispose) reader).Dispose ();
}
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"INeedADip" <in*******@gmai l.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Can anyone here confirm for sure that reader.Close() and Dispose() will
get called using the following routine:

using(SqlDataRe ader reader = GetOpenReaderFu nction())
{
while(reader.Re ad()) doSomething(rea der);
}

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*******@gmai l.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Can anyone here confirm for sure that reader.Close() and Dispose() will
get called using the following routine:

using(SqlDataRe ader reader = GetOpenReaderFu nction())
{
while(reader.Re ad()) doSomething(rea der);
}

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(SqlDataRe ader reader = GetOpenReaderFu nction())
{
while(reader.Re ad()) doSomething(rea der);
}

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*******@gmai l.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Can anyone here confirm for sure that reader.Close() and Dispose() will
get called using the following routine:

using(SqlDataRe ader reader = GetOpenReaderFu nction())
{
while(reader.Re ad()) doSomething(rea der);
}

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.Exec uteReader(
CommandBehavior .CloseConnectio n );

--
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 CommandBehaviou r 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*******@gmai l.com> wrote:
Can anyone here confirm for sure that reader.Close() and Dispose() will
get called using the following routine:

using(SqlDataRe ader reader = GetOpenReaderFu nction())
{
while(reader.Re ad()) doSomething(rea der);
}

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 GetOpenReaderFu nction() acquired a
SqlDataReader but then threw an exception before returning it.

--
Jon Skeet - <sk***@pobox.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
INeedADip <in*******@gmai l.com> wrote:
Can anyone here confirm for sure that reader.Close() and Dispose() will
get called using the following routine:

using(SqlDataRe ader reader = GetOpenReaderFu nction())
{
while(reader.Re ad()) doSomething(rea der);
}

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 GetOpenReaderFu nction() acquired a
SqlDataReader but then threw an exception before returning it.

--
Jon Skeet - <sk***@pobox.co m>
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@prio rganize_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.co m>
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1477
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 the choice of a > keyword implies that not taking a stance leaves those people with no > basis for making their decision. As I see it, there are two camps to whom we need to be "fair" in this: 1) Those who would reject the syntax if it used an...
28
20343
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()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object doesn't support this property or method." in IE, and nothing happens in Firebird.
1
433
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 bunch of people said what if exception occurs buffered stream is not closed. My Argument is over advantages of using 'Using' keyword. First Advantage being -- Using used for Dispose (Deterministic
11
19461
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 base class) then override(in the derived class), but fails to compare and contrast the two... I've read this (short snippet)three times and by the looks of it they both do exactly the same thing- allow you to derive from the base and save...
7
3015
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 "Close()" methods reared their ugly (at least it would seem...)heads. I was happily delving away in the .NET framework, investigating the stream classes with the msdn and Lutz Roeder's .NET reflector, when I stumbled upon the following:
3
1471
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 File1.cs of the following code. Notice that when the using keyword is placed outside of the namespace decleration its behavior is different than placing it inside the namespace decleration. Questions: 1) Section 9.3.2 of the specification...
3
2005
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 application. If you have not used the keyword 'Overridable' then read on for sure... If you setup a child class and you want to override a method in your base class, you may see the squiggles under your child's method name. The pop-up/build error says...
11
3162
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 DRAWSTRING function to place the barcode on the image, but no matter what graphics settings (.InterpolationMode/.CompositingQuality etc.) I manipulate, the barcode image remains poor quality. For exaple I'm using a solidbrush that is black, but some of...
3
12627
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
2349
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 should work like if I give more than one keyword then it should not give me the redundant data..If data is same for more than one keyword then it should show it only once..and here is the code I have so far to search using one keyword.. print...
0
9464
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10289
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8952
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7471
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6722
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2860
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.