473,320 Members | 1,732 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,320 software developers and data experts.

Is there any difference between these two code?

hi, all
I am just wondering if there is some difference between these two code:
I:
using(SqlDbConnection con = new .....)
{
SqlDbCommand cmd = new SqlDbCommand(strQuery, con);
cmd.ExecuteNonQuery();
}
Once the program exit the using }, the con should be disposed.

II:
SqlDbConnection con = null;
using(con = new .....)
{
SqlDbCommand cmd = new SqlDbCommand(strQuery, con);
cmd.ExecuteNonQuery();
}
Here, will it do the same thing as I? It might think there is still a
reference.

Nov 17 '05 #1
6 3139

--------------------------------------------------------------------------------
All that glitters has a high refractive index.
www.mendhak.com
"Nick" <ni*******@yahoo.com.cn> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
hi, all
I am just wondering if there is some difference between these two code:
I:
using(SqlDbConnection con = new .....)
{
SqlDbCommand cmd = new SqlDbCommand(strQuery, con);
cmd.ExecuteNonQuery();
}
Once the program exit the using }, the con should be disposed.

II:
SqlDbConnection con = null;
using(con = new .....)
{
SqlDbCommand cmd = new SqlDbCommand(strQuery, con);
cmd.ExecuteNonQuery();
}
Here, will it do the same thing as I? It might think there is still a
reference.

Nov 17 '05 #2

Nick,
There is no difference between the two. In both cases, the objects are
disposed of when the } is hit.

HTH,
S.M. Altaf
[MVP - VB]
--------------------------------------------------------------------------------
All that glitters has a high refractive index.
www.mendhak.com

"Nick" <ni*******@yahoo.com.cn> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
hi, all
I am just wondering if there is some difference between these two code:
I:
using(SqlDbConnection con = new .....)
{
SqlDbCommand cmd = new SqlDbCommand(strQuery, con);
cmd.ExecuteNonQuery();
}
Once the program exit the using }, the con should be disposed.

II:
SqlDbConnection con = null;
using(con = new .....)
{
SqlDbCommand cmd = new SqlDbCommand(strQuery, con);
cmd.ExecuteNonQuery();
}
Here, will it do the same thing as I? It might think there is still a
reference.

Nov 17 '05 #3
S.M. is correct, both objects are disposed of when the using block ends. I
would like to add though that the difference is that you do have a reference
to an disposed of object at the end of the second example, a reference that
will never turn null on it’s own and trying to use it can be dangerous...
even though it is still accessible.

As an example, throw in Console.WriteLine( con.WorkstationId ) at the end of
the second using block and you should see your workstation id/name returned
from this disposed of object.

Brendan
"Nick" wrote:
hi, all
I am just wondering if there is some difference between these two code:
I:
using(SqlDbConnection con = new .....)
{
SqlDbCommand cmd = new SqlDbCommand(strQuery, con);
cmd.ExecuteNonQuery();
}
Once the program exit the using }, the con should be disposed.

II:
SqlDbConnection con = null;
using(con = new .....)
{
SqlDbCommand cmd = new SqlDbCommand(strQuery, con);
cmd.ExecuteNonQuery();
}
Here, will it do the same thing as I? It might think there is still a
reference.

Nov 17 '05 #4
Got it.
Thank you very much.

Nov 17 '05 #5
Something I like more, since SqlCommand also needs disposing (ok, it has a
Dispose() method...) is the following:

using(SqlConnection conn = new ...)
using(SqlCommand cmd = conn.CreateCommand())
{
...
}

That way (i) the command is created from the connection and both are
disposed at the proper time and (ii) no references are left to disposed
objects (as another reply mentioned.

my 2cents
scott

"Nick" <ni*******@yahoo.com.cn> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Got it.
Thank you very much.

Nov 17 '05 #6
Hi,

As the other posters stated both forms are valid and in both cases the
object is disposed , now I highly recommend you do not use the second
version, you expose the variable out of the scope of the using, and up to
the method. This may cause problems:
SqlDbConnection con = null;
using(con = new .....)
{
SqlDbCommand cmd = new SqlDbCommand(strQuery, con);
cmd.ExecuteNonQuery();
}
cmd.ExecuteScalar(); // will give you error

The first variant will cause a compiler error in that line.
you should always declare the variable in the using statement, IIRC Fxcop
will gives you a warning in this situation.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


"Nick" <ni*******@yahoo.com.cn> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
hi, all
I am just wondering if there is some difference between these two code:
I:
using(SqlDbConnection con = new .....)
{
SqlDbCommand cmd = new SqlDbCommand(strQuery, con);
cmd.ExecuteNonQuery();
}
Once the program exit the using }, the con should be disposed.

II:
SqlDbConnection con = null;
using(con = new .....)
{
SqlDbCommand cmd = new SqlDbCommand(strQuery, con);
cmd.ExecuteNonQuery();
}
Here, will it do the same thing as I? It might think there is still a
reference.

Nov 17 '05 #7

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

Similar topics

19
by: Raj Dhrolia | last post by:
Hi Guys, It might seem to be a very easy question, but i am very much eager to know some good technical difference between C# and VB.NET. Are there anything that i can do in one language and...
11
by: Shea Martin | last post by:
I have been programming in C++ for over 4 years. I *think* I knew that a struct could have a constructor but I decided to dig into it a little more today, and found that there is very little...
6
by: deko | last post by:
Is there a difference between the Form_Open and Form_Load events? When should I use one rather than the other? I have several forms that require code to run when they open... or is it when they...
17
by: Nathan Given | last post by:
Hello All, I am trying to debug a broken query. The query uses Left$(,4) instead of Left(,4). What is the difference between the Left() and Left$() functions in Microsoft Access? Thanks!...
24
by: wwj | last post by:
Hi ,all I want to know the difference between char a and char *p=new char and the difference between the heap and the stack ,and if the char a is corresponding to the stack in MEMORY,and char...
19
by: Jazper Manto | last post by:
hi i read thousands of millions of articles about STA and MTA :-). everything very theoretical and i don't get it... could anybody explain me the and on a nice, small and simple example where...
12
by: Nathan Sokalski | last post by:
What is the difference between the Page_Init and Page_Load events? When I was debugging my code, they both seemed to get triggered on every postback. I am assuming that there is some difference,...
7
by: Bart_D | last post by:
Hi, Can anybody explain me what's the difference between for example: imports system.data implements ICallbackEventHandler inherits System.Web.UI.Page Thanks Bart
13
by: Jon Slaughter | last post by:
Is there any difference between these "languages" any more or are they just syntactically different but represent the same thing? A long time ago VB was more for "noobs" while something like C++...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.