Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Connection to database not closing...

Status
Not open for further replies.

lidds

Programmer
Jun 9, 2005
72
GB
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:

Code:
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

Code:
        ' 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
 
Your test does not make sense to me. How do you interpret it?

If you are checking to see if it is diconnected
1) if you pass in a bad name of a database it will not connect, passed the if check, and returns false
2) if you pass in a good name of a database it will connect and fail the if check. Then it will close and then return true.

So in the first case it is disconnected and returns false, the second case it is disconnected and returns true.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top