Connection to database not closing...

lidds

Well-known member
Joined
Oct 19, 2004
Messages
122
Programming Experience
Beginner
I am stuggling to find out what I am doing wrong here and think a fresh pair of eyes might help.

I have a class called DBAccess which I am using to connect to a SQL Server database, the DBAccess class contains the following code:

VB.NET:
Public Class DBAccess

    Private connStr As String = Nothing
    Public myConn As New SqlClient.SqlConnection

    Public Sub New()

    End Sub

    Public Function Connect(ByVal DBName As String) As Boolean
        ' connect to database
        connStr = "Data Source=myServer;initial catalog='" & DBName & "';user id=myUsername;password=myPassword"        
        myConn.ConnectionString = connStr

        Try
            myConn.Open()
            Return True
        Catch e As Exception
            MsgBox(e.ToString)
            Return False
        End Try
    End Function

    Public Function DisConnect() As Boolean
        ' connect to database
        myConn.Close()
        Return True 
    End Function
End Class

I then am calling this class using the below code

VB.NET:
        ' Check connection to database
        Dim myDB As New DBAccess()
        If myDB.Connect("myDatabaseName") = False Then
            Return False
        Else
            myDB.DisConnect()
            Return True
        End If

The problem that I have is this seems to not close the connect for some reason and I don't know why. Does anyone know why this might be?

Thanks in advance

Simon
 
Back
Top