连接到数据库而不是在VB.NET中关闭

时间:2013-12-12 09:59:10

标签: sql vb.net oledb

我有以下功能。有时在使用它之后我需要删除数据库并重新创建它,但是这个函数继续使用数据库(所以它不能被删除),即使我要求它删除数据库并关闭所有连接。非常感谢任何帮助。

Public Function alreadyindatabase(ByVal url As String) As Boolean
    url = url.Replace("'", "''")
    Dim connetionString As String
    Dim oledbCnn As OleDbConnection
    Dim oledbCmd As OleDbCommand
    Dim sql As String

    connetionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & datafile
    sql = "Select * from visitedurl Where [Field1]='" + url + "'"

    oledbCnn = New OleDbConnection(connetionString)
    Try
        oledbCnn.Open()
        oledbCmd = New OleDbCommand(sql, oledbCnn)
        Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()

        If oledbReader.Read Then
            'MsgBox("Found")
            Return True
        Else
            'MsgBox("Not found")
            Return False
        End If
        'While oledbReader.Read
        ' MsgBox(oledbReader.Item(0))
        'End While
        SqlConnection.ClearAllPools()
        sql = "DROP DATABASE [" & datafile & "]"
        oledbCmd = New OleDbCommand(sql)
        oledbCmd.ExecuteNonQuery()

        oledbCmd.Cancel()
        oledbReader.Close()
        oledbCmd.Connection.Close()
        oledbCnn.Close()
        oledbCmd.Dispose()
        oledbCnn.Dispose()


    Catch ex As Exception

    End Try


End Function

4 个答案:

答案 0 :(得分:3)

你应该在try catch中有一个finally部分并关闭其中的连接。否则,如果发生异常,则连接不会关闭。

如果你看一下这个答案(https://stackoverflow.com/a/8557227/465404),它会告诉你如何。

所以你可能希望它与此类似......

Public Function alreadyindatabase(ByVal url As String) As Boolean
    url = url.Replace("'", "''")
    Dim connetionString As String
    Dim oledbCnn As OleDbConnection
    Dim oledbCmd As OleDbCommand
    Dim sql As String

    connetionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & datafile
    sql = "Select * from visitedurl Where [Field1]='" + url + "'"

    oledbCnn = New OleDbConnection(connetionString)
    Try
        oledbCnn.Open()
        oledbCmd = New OleDbCommand(sql, oledbCnn)
        Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()

        If oledbReader.Read Then
            'MsgBox("Found")
            Return True
        Else
            'MsgBox("Not found")
            Return False
        End If
        'While oledbReader.Read
        ' MsgBox(oledbReader.Item(0))
        'End While
        SqlConnection.ClearAllPools()
        sql = "DROP DATABASE [" & datafile & "]"
        oledbCmd = New OleDbCommand(sql)
        oledbCmd.ExecuteNonQuery()

        oledbCmd.Cancel()
        oledbReader.Close()
        oledbCmd.Connection.Close()
        oledbCnn.Close()
        oledbCmd.Dispose()
        oledbCnn.Dispose()


    Catch ex As Exception
        MsgBox(ex.ToString)
    Finally
         If oledbCnn.State = ConnectionState.Open Then oledbCnn.Close()
    End Try


End Function

答案 1 :(得分:0)

删除try catch。你可能得到一个异常,然后忽略它,这意味着连接永远不会关闭,你永远不会发现。或者,您可以在关闭连接的位置添加finally子句,因为无论如何都会运行。

答案 2 :(得分:0)

您正在关闭连接两次:

oledbCmd.Cancel()
oledbReader.Close()
oledbCmd.Connection.Close()  '<--------
oledbCnn.Close()             '<--------
oledbCmd.Dispose() 
oledbCnn.Dispose()  

可能,它正在生成一个阻止连接Dispose调用的异常。

尝试评论第一次连接关闭尝试(oledbCmd.Connection.Close())

此致

答案 3 :(得分:0)

如果出现错误,您的连接将不会被关闭。您可以将相关部分包装在Using - 块中(基本上会自动添加Try-Finally),并且还会缩短您的代码。:

Public Function alreadyindatabase(ByVal url As String) As Boolean
    url = url.Replace("'", "''")
    Dim connetionString As String
    Dim sql As String

    connetionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & datafile
    sql = "Select * from visitedurl Where [Field1]='" + url + "'"

    Using oledbCnn = New OleDbConnection(connetionString)
        oledbCnn.Open()
        Using oledbCmd = New OleDbCommand(sql, oledbCnn)
            Using oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()
            If oledbReader.Read Then
                Return True
            Else
                Return False
            End If
        End Using
        SqlConnection.ClearAllPools()
        sql = "DROP DATABASE [" & datafile & "]"
        Using oledbCmd = New OleDbCommand(sql)
            oledbCmd.ExecuteNonQuery()
            oledbCmd.Cancel()
        End Using
    End Using
End Function