Dispose warnings on SqlAdapter code

I am rewriting a .NET 1.1 app to 4.0.  This code compiles just fine, but I receive numerous warnings  like:
"call System.IDispoable.Dispose on object 'sqlCmd' before all references to it are out of scope".
I also get that warning for da and results.
Is it really important that I clean that up?  And how would I "dispose" of these?  I thought the garbage collector does that for me.  
Thank you for helping!
    Public Function SelectTsql(ByVal qryString As String) As DataTable
        Dim sqlCmd As SqlCommand

        Dim da As SqlDataAdapter
        Dim results As DataTable

        sqlCmd = New SqlCommand

        With sqlCmd
            '.Transaction = transaction
            .CommandTimeout = 240
            .CommandText = qryString
            If _sqlInSrvCnn1.State = ConnectionState.Open Then
                .Connection = _sqlInSrvCnn1
                .Connection = _sqlInSrvCnn2
            End If
            .CommandType = CommandType.Text
            da = New SqlDataAdapter(sqlCmd)
            results = New DataTable
        End With

        If results.Rows.Count > 0 Then
            Return results
            Return Nothing
        End If

    End Function

Open in new window

Who is Participating?
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
The DataAdapter and SqlCommand objects both inherit a Dispose() method, which you should call to release the non-managed resources used by the object.

It is good practice to clean up after yourself. The garbage collector will do it for you - eventually - but you should do it yourself too. Simple rule: if the object supports Dispose(), you should call it when you're done with the object.

A good habit to get into would be using a "Using" statement to wrap any database objects. That way they will automatically be disposed when they fall out of scope: http://msdn.microsoft.com/en-us/library/htd05whh.aspx
ToolTimeGangAuthor Commented:
Thank you!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.