2013-12-12 3 views
0

다음과 같은 기능이 있습니다. 때로는 그것을 사용 후 데이터베이스를 삭제하고 다시 만들 필요가 있지만이 함수는 데이터베이스를 삭제하고 데이터베이스를 삭제하고 모든 연결을 닫을 것을 요청하더라도 데이터베이스를 계속 사용합니다. 어떤 도움이라도 대단히 감사하겠습니다.VB.NET에서 데이터베이스 연결이 닫히지 않습니다.

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 

답변

0

try catch를 제거하십시오. 당신은 아마도 예외를 얻었을 것입니다, 그리고 그것을 무시하십시오. 이것은 연결이 결코 닫히지 않았고 당신이 결코 알지 못했다는 것을 의미합니다. 또는 연결을 닫을 때 finally 절을 추가 할 수 있습니다.

2

마침내 섹션을 잡아서 거기에서 연결을 닫아야합니다. 그렇지 않으면 예외가 발생하면 연결이 닫히지 않습니다.

이 대답 (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

일을 단축 (기본적으로 시도 - 마지막으로 자동으로 추가됩니다). 나는 동의한다. 'Try'에서 close 연결을 두 번 제거하는 것도 도움이 될 수 있습니다. – equisde

+0

고마워요. 이것이 이유라고 생각했지만 시도를하지 않고 oledbCnn.Close()를 입력하면 문제가 해결되지 않았습니다. 확실하지 그 사이의 차이점을 사용하여 마지막으로 어쩌면 내가. NET의 기본 브러시해야합니다. – Bottopia

+0

try가 성공적으로 실행되었거나 예외가 발생했는지 여부에 관계없이 finally 섹션이 실행됩니다. 이것은 데이터베이스 연결을 닫거나 파일 핸들러를 닫는 것과 같이 자원을 정리해야하는 장소에 매우 유용합니다. 귀하의 질문에 성공적으로 답변 한 경우 답변을 표시해야합니다. – Satal

0

:

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

을 어쩌면, 그것은 연결이 폐기 방지 예외를 생성 요구.

시도가 첫 번째 연결 가까운 시도 드 언급하는 (oledbCmd.Connection.Close())

감사

0

에 대한 연결이 오류의 경우에는 폐쇄되지 않습니다. 당신은 Using - 블록의 관련 섹션을 포장 할 수 있으며 그에 의해 또한 코드 :

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 
관련 문제