ado.net managing connections

Posted by madlan on Stack Overflow See other posts from Stack Overflow or by madlan
Published on 2010-06-06T11:09:43Z Indexed on 2010/06/06 11:12 UTC
Read the original article Hit count: 358

Filed under:
|
|

Hi,

I'm populating a listview with a list of databases on a selected SQL instance, then retrieving a value from each database (It's internal product version, column doesn't always exist) I'm calling the below function to populate the second column of the listview:

item.SubItems.Add(DBVersionCheck(serverName, database.Name))

Function DBVersionCheck(ByVal SelectedInstance As String, ByVal SelectedDatabase As String)
    Dim m_Connection As New SqlConnection("Server=" + SelectedInstance + ";User Id=sa;Password=password;Database=" + SelectedDatabase)
    Dim db_command As New SqlCommand("select Setting from SystemSettings where [Setting] = 'version'", m_Connection)
    Try
        m_Connection.Open()
        Return db_command.ExecuteScalar().trim
        m_Connection.Dispose()
    Catch ex As Exception
        'MessageBox.Show(ex.Message)
        Return "NA"
    Finally
        m_Connection.Dispose()
    End Try
End Function

This works fine except it's creating a connection to each database and leaving it open. My understanding is the close()\dispose() releases only the connection from the pool in ado rather than the actual connection to sql.

How would I close the actual connections after I've retrieved the value? Leaving these open will create hundreds of connections to databases that will probably not be used for that session.

© Stack Overflow or respective owner

Related posts about sql

Related posts about vb.net