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.