VB2005 best practice with DataAccessAdapter

Posts   
 
    
omar avatar
omar
User
Posts: 569
Joined: 15-Oct-2004
# Posted on: 15-Apr-2006 17:08:46   

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?? 2- what will happen (in VB2005 case) if an exception is thrown in the "using" block ? 3- does it make a difference if we declare ths "using" variable as IDataAccessAdapter or DataAccessAdapter?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 15-Apr-2006 19:11:45   

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.

Frans Bouma | Lead developer LLBLGen Pro