question about the using statement: datareaders, and connections

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

consider these two using statements:
using (SqlConnection sqlcon1 = new SqlConnection(constr))
{ ...}
and
if (this.rbl_LTorST.SelectedValue == "0")
rdr = rfcs.fn_SpecData();
else
rdr = rfcs.fn_SpecData2();
using (rdr)
{...}
When an excption occurs the error message is still displayed as normal, but
the object is removed as if it were traped in a try catch finally?
just want to be sure.
also: is there a time when in is not advisable to use a uning or a tray
catch finally?
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes
 
consider these two using statements:
using (SqlConnection sqlcon1 = new SqlConnection(constr))
{ ...}
and
if (this.rbl_LTorST.SelectedValue == "0")
rdr = rfcs.fn_SpecData();
else
rdr = rfcs.fn_SpecData2();
using (rdr)
{...}
When an excption occurs the error message is still displayed as normal, but
the object is removed as if it were traped in a try catch finally?
just want to be sure.

Well, the object isn't "removed" as such - but Dispose is called on
it, allowing for resource clean-up.
also: is there a time when in is not advisable to use a uning or a tray
catch finally?

Not really sure what you're asking - but whenever you want to call
Dispose however control leaves a block, the "using" statement is
handy.

Jon
 
WebBuilder451 said:
consider these two using statements:
using (SqlConnection sqlcon1 = new SqlConnection(constr))
{ ...}
and
if (this.rbl_LTorST.SelectedValue == "0")
rdr = rfcs.fn_SpecData();
else
rdr = rfcs.fn_SpecData2();
using (rdr)
{...}
When an excption occurs the error message is still displayed as normal,
but
the object is removed as if it were traped in a try catch finally?

The object is disposed, which is the intention of the using construction.
Frankly I always use the first usage.
just want to be sure.
also: is there a time when in is not advisable to use a uning or a tray
catch finally?

I don't see the relation between both instructions, "using" is one thing and
try/catch is a completely different thing.

You use try/catch to handle errors, you use "using" as a shortcut to call
dispose.
Now you will see a lot of docs that says that
using( SqlConnection sqlcon1 = new SqlConnection()){}

is equivalent to

SqlConnection sqlcon1
try
{
sqlcon1 = new SqlConnection();
}
catch{
throw;
}
finally
{
sqlcon1.Dispose();
}


They are not the same in reality. There are a number of differences like in
scope.

What they mean is that you must use wrap the usage of the isntance in a
try/catch/finally to assure that the instance be disposed.
Only that.
 
i did see a lot of docs and it did not make sense to me that they were the
same. This was the reason for the question. I would like to make sure on this
one point:

T/F The using statement will NOT close a connection or reader or, ... on an
error.
Thank You
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes
 
I asked the same followup question to Ignacio:
T/F The Using statement will NOT close a connection, reader, or whater, if
an error occures or is it ONLY a shorthand for calling dispose?
I appreciare your patience, but there seems to be some confusion in some of
the docs i've read online?

Thank You
--
(i''ll be asking a lot of these, but I find C# totally way cooler than vb
and there''s no go''n back!!!)
thanks (as always)

kes
 
Hi,

WebBuilder451 said:
i did see a lot of docs and it did not make sense to me that they were the
same. This was the reason for the question. I would like to make sure on
this
one point:

T/F The using statement will NOT close a connection or reader or, ... on
an
error.

It will close the connection IF the connection is closed when the object is
disposed. In the case of the different SqlXXXX classes it is.
But if you define your own class it's your responsability to do so.


Another good thing about the using is that the scope of the variable is only
inside the using. It does not exist outside.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top