473,663 Members | 2,743 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 2123
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
1472
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
20309
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
19451
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
3010
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
1469
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
2000
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
3135
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
12620
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
2339
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
8436
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8771
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...
0
7371
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
6186
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
5657
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();...
0
4182
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4349
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2763
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1757
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.