Hi
I just would like to know what the most appropriate techniques are for
Disposal of ADO.NET objects
ie, consider the following code (VB):
(It just creates and returns a dataset)
Public Function GetDataSet() As DataSet
Dim conn As New SqlConnection(connString)
Dim cmdSelect As New SqlCommand("mySProc", conn)
cmdSelect.CommandType = CommandType.StoredProcedure
Dim ds As New DataSet()
Dim da As SqlDataAdapter = New SqlDataAdapter(cmdSelect)
da.Fill(ds, "TableName")
da.Dispose()
cmdSelect.Dispose()
conn.Dispose()
Return ds
End Function
Basically..Am I going overkill on the Dispose methods?
(ie if I Dispose the DataAdaptor, does it dispose it's Command objects
itself, therefore meaning that I don't have to), etc
Also...What resources are actually being disposed?
The only resource I would have thought needed disposing, was the database
connection (and therefore conn.Dispose would do the entire job
Finally...(and on a related note) would the caller have to dispose the
dataset (seeing that at that point it's basically just an in-memory XML
stream)
I can't find anything in the docs about it (just that Dispose should be
called if it exists, and you're finished with it) and the ADO.NET books I've
looked through rarely call Dispose at all (perhaps with the same reasoning
that error handling is rarely shown in books..to make the point of the
example clear)
TIA
Merak
You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.