I find it best to use a Try Catch block for data access and the close the connection in the Finally block. The Finally block executes at the end of normal execution or before exception moves on if there is an exception so it is a good fail safe way of making sure you clean up your connection.
VB Code:
Dim cnn As SQLConnection Try cnn=New SQLConnection("myconnectionstring") cnn.Open() 'my data operation Catch ex As Exception 'my exception handling Finally If Not cnn Is Nothing AndAlso cnn.State=Open Then cnn.Close() End If End Try
PS This is a good idea regardless of the Tiers you use or using the Application Blocks are good as well.




Reply With Quote