473,404 Members | 2,137 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,404 software developers and data experts.

using statment with sql connection

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
Nov 17 '05 #1
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

Nov 17 '05 #2
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

Nov 17 '05 #3
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
Nov 17 '05 #4
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


Nov 17 '05 #5
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


Nov 17 '05 #6
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

Nov 17 '05 #7
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
Nov 17 '05 #8

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

Similar topics

2
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...
18
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...
3
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...
5
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....
4
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...
4
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...
2
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...
1
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...
3
by: CSharper | last post by:
I have a using statement something like the following try { using(SqlConnection cnn = ...) { } catch(Exception e1) { }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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...
0
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...
0
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...

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.