omar wrote:
VB2003 code:
adapter = DataAdapterFactory.CreateDataAccessadapter(True)
Try
'... some code
Catch
Throw
Finally
adapter.CloseConnection()
adapter.Dispose()
End Try
Now, with VB2005's "using":
Using da As IDataAccessAdapter = DataAdapterFactory.CreateDataAccessadapter
'... some code
End Using
1- Does the new "using" guarantee closing the adapter's connection AND disposing the adapter object as did the FINALLY clause in the first code sample??
Yes. It calls dispose which calls closeconnection, rollback transaction (if any) and cleans up everything.
2- what will happen (in VB2005 case) if an exception is thrown in the "using" block ?
using(...)
simply emits a try/finally block around your code and calls dispose on the object in the finally block, so it actually creates the code you wrote in vs.net 2003.
3- does it make a difference if we declare ths "using" variable as IDataAccessAdapter or DataAccessAdapter?
No.