about the using

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hello!

Here I use the using so an implicit close is automatically invoked.
using (TextWriter tw = new StreamWriter(new FileStream(STATISTIK,
FileMode.Append, FileAccess.Write)))
tw.WriteLine(myProduct + "," + year + ","+ month);

Now to my question is it possible to use the using in this case so I don't
have to use the close command
SqlConnection thisConnection = new SqlConnection(...);
SqlDataAdapter thisAdapter = new SqlDataAdapter (...);
....
....
thisConnection.Close();

//Tony
 
Strictly speaking the using statement causes the object in scope to be
disposed (so the object must implement IDisposable), in disposing the object
Close gets called.

Yes it's completely possible (and advisable) to use SqlConnection's within a
using scope so that connections are freed up as soon as possible.
 
Hello!

This SqlConnection doesn't implement this IDisposable
but as I mentioned this TextWriter does implement IDisposable.

So accoding to this I can't use the using construction on SqlConnection do
make the object be disposed

//Tony
 
Hello!

This SqlConnection doesn't implement this IDisposable
but as I mentioned this TextWriter does implement IDisposable.

SqlConnection DOES implement IDisposable
So accoding to this I can't use the using construction on SqlConnection do
make the object be disposed

Except that it does implement it :)
 

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