I'm refactoring legacy code and I came accros alot of code like this, and I want to know if I must dispose of IDbParameter:
Private Function GetSqlObject as Object
Try
Dim parmeters() As IDbDataParameter = CreateParameters(criterias)
reqSQL = "A SQL Request"
dataReader = DBHelper.ExecuteReader(CommandType.Text, reqSQL, parmeters)
While dataReader.Read()
....
End While
Return ...
Finally
DisposeParameters(parm)
If Not dataReader Is Nothing Then
dataReader.Close()
dataReader.Dispose()
End If
End Try
End Sub
Private Sub DisposeParameters(ByVal parameters As IDbDataParameter())
If parameters Is Nothing Then Return
Dim dbDataParameterArray As IDbDataParameter() = parameters
Dim index As Integer = 0
While index < dbDataParameterArray.Length
dbDataParameterArray(index).Dispose()
index += 1
End While
End Sub
I wonder if I can completly remove the Finally statement just by a using:
Using dataReader = DBHelper.ExecuteReader(CommandType.Text, reqSQL, parmeters)
While dataReader.Read()
....
End While
End Using
In that case, I know that the dataReader as stated here : correct point to close a data reader in vb.net
But how about the parameters ? Do I need to dispose them ? Is it just safe to let them garbage collected and remove completly the Finally and DisposeParameters
function ?
I have a little confusion about reading this article: Proper use of the IDisposable interface
Since the parameters is not a DB connection or such, I think it's managed ? If so, I don't need to dispose it ?