I was wondering about the follow code. I know that dispose is called on an
object when it leaves the using clause but was also wondering if the closed
statement was called on objects like Sql Connection and Data Readers?
Basically will I have problem or leave an object in memory if the closed is
not called below?
using(SqlConnection con = new SqlConnection())
{
con.Open();
using(SqlCommand cmd = con.CreateCommand)
{
... set up
using(SqlDataReader dr = cmd.ExecuteDateReader())
{
while(dr.Read())
..... do something
}
}
}
Regards,
John 7 3455
John, in both cases the object's Close( ) methods are called in their
respective Dispose( ) methods. In a situation where you don't wrap your code
in using statements, then I suppose not calling the Close methods of these
objects only become a problem if they are long lived or objects scoped as
members of some class that is long lived, otherwise when the objects fall
out of scope they should be disposed of.
Hope this helps.
Cordell Lawrence
Teleios Systems Ltd.
"John J. Hughes II" <no@invalid.com> wrote in message
news:eJ****************@TK2MSFTNGP10.phx.gbl... I was wondering about the follow code. I know that dispose is called on
an object when it leaves the using clause but was also wondering if the
closed statement was called on objects like Sql Connection and Data Readers? Basically will I have problem or leave an object in memory if the closed
is not called below?
using(SqlConnection con = new SqlConnection()) { con.Open(); using(SqlCommand cmd = con.CreateCommand) { ... set up using(SqlDataReader dr = cmd.ExecuteDateReader()) { while(dr.Read()) ..... do something } } }
Regards, John
Hi,
The close operation will be performed when the Dispose event, now as this
is not deterministic you don't have control of when will that happen,
therefore it's advised that you close your connectin as soon as you finish
with it.
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"John J. Hughes II" <no@invalid.com> wrote in message
news:eJ****************@TK2MSFTNGP10.phx.gbl... I was wondering about the follow code. I know that dispose is called on an object when it leaves the using clause but was also wondering if the closed statement was called on objects like Sql Connection and Data Readers? Basically will I have problem or leave an object in memory if the closed is not called below?
using(SqlConnection con = new SqlConnection()) { con.Open(); using(SqlCommand cmd = con.CreateCommand) { ... set up using(SqlDataReader dr = cmd.ExecuteDateReader()) { while(dr.Read()) ..... do something } } }
Regards, John
Ignacio Machin ( .NET/ C# MVP ) wrote: Hi,
The close operation will be performed when the Dispose event, now as this is not deterministic you don't have control of when will that happen, therefore it's advised that you close your connectin as soon as you finish with it.
?? I thought Disposed was deterministic, only finalize is not
deterministic. In his example Dispose should be called when the using block
is exited.
--
Tom Porterfield
Thanks,
John
"Cordell Lawrence" <cl*******@teleios-systems.com> wrote in message
news:eD**************@TK2MSFTNGP09.phx.gbl... John, in both cases the object's Close( ) methods are called in their respective Dispose( ) methods. In a situation where you don't wrap your code in using statements, then I suppose not calling the Close methods of these objects only become a problem if they are long lived or objects scoped as members of some class that is long lived, otherwise when the objects fall out of scope they should be disposed of.
Hope this helps. Cordell Lawrence Teleios Systems Ltd.
"John J. Hughes II" <no@invalid.com> wrote in message news:eJ****************@TK2MSFTNGP10.phx.gbl... I was wondering about the follow code. I know that dispose is called on an object when it leaves the using clause but was also wondering if the closed statement was called on objects like Sql Connection and Data Readers? Basically will I have problem or leave an object in memory if the closed is not called below?
using(SqlConnection con = new SqlConnection()) { con.Open(); using(SqlCommand cmd = con.CreateCommand) { ... set up using(SqlDataReader dr = cmd.ExecuteDateReader()) { while(dr.Read()) ..... do something } } }
Regards, John
Ok, I had a feeling about that. Basically I could end up with a bunch of
open connection waiting for the system to get around to closing them which
could be a problem.
Thanks,
John
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2***************@TK2MSFTNGP15.phx.gbl... Hi,
The close operation will be performed when the Dispose event, now as this is not deterministic you don't have control of when will that happen, therefore it's advised that you close your connectin as soon as you finish with it.
cheers,
-- Ignacio Machin, ignacio.machin AT dot.state.fl.us Florida Department Of Transportation
"John J. Hughes II" <no@invalid.com> wrote in message news:eJ****************@TK2MSFTNGP10.phx.gbl...I was wondering about the follow code. I know that dispose is called on an object when it leaves the using clause but was also wondering if the closed statement was called on objects like Sql Connection and Data Readers? Basically will I have problem or leave an object in memory if the closed is not called below?
using(SqlConnection con = new SqlConnection()) { con.Open(); using(SqlCommand cmd = con.CreateCommand) { ... set up using(SqlDataReader dr = cmd.ExecuteDateReader()) { while(dr.Read()) ..... do something } } }
Regards, John
Hi Tom,
My mistake, you are 100% right, Dispose is deterministic
sorry for the error, going to get some expresso in my bloodstream :)
cheers,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Tom Porterfield" <tp******@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... Ignacio Machin ( .NET/ C# MVP ) wrote: Hi,
The close operation will be performed when the Dispose event, now as this is not deterministic you don't have control of when will that happen, therefore it's advised that you close your connectin as soon as you finish with it.
?? I thought Disposed was deterministic, only finalize is not deterministic. In his example Dispose should be called when the using block is exited. -- Tom Porterfield
So basically if I can stop writing close on these short read commands,
that's nice, saves a lot of finally statments in my code.
Regards,
John This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: mr_burns |
last post by:
hi,
how do i do a SELECT statment that will just query a table to find the
row with the maximum of a declared field. for example, if i have a
table of customers and i would like to get the value...
|
by: Ian Davies |
last post by:
Hello
Apologies for posting to so many groups but this one is difficult to
catagorize precisely
The following code worked fine with a connection to an Access database.
However, I have changed...
|
by: claus.hirth |
last post by:
When I execute the following statment using the Command Center it
works:
LOAD CLIENT FROM 'Y:\\TheFixedWidthData.txt' OF ASC METHOD L ( 1 1,2
11,12 17) REPLACE INTO S00.TheTargetTable...
|
by: charliewest |
last post by:
I've implemented the USING statement to ensure that my newly created sql
connection closes when my method is finished using it. The USING statement is
wrapped in try/catch error handling statement....
|
by: semsem22 |
last post by:
hello, i m new to ASP.NET, but have experience with VB6....i have a couple of questions about the use of a module and the decleration of public objects in the module vs the use of sessions
the...
|
by: Agnes |
last post by:
I searched from previous post and got that there is a solution which export
the data to export without using Excel auotmation.
However, my database is SQL server, How can I amend...
|
by: mc |
last post by:
I'm writing a C# asp.net webpage.
I have a SQL connection and command inside using statements (as below),
if I do a response.redirect inside it will the auto-disposal code execute?
using...
|
by: zeebiggie |
last post by:
Good morning
I have a form with the controls in the insert statment below. table1 has an Auto increment primary key hence is omitted in the insert statment and form. Am getting the error It didnt...
|
by: CSharper |
last post by:
I have a using statement something like the following
try
{
using(SqlConnection cnn = ...)
{
}
catch(Exception e1)
{
}
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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...
|
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,...
|
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: 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...
|
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...
| |