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 :)
 
Back
Top