不关闭数据连接的严重后果

来源:互联网 发布:java图形界面教程 编辑:程序博客网 时间:2024/06/11 22:12

做了一个DATAGRID,要按两次删除才能在屏幕上显示删除后的结果,整了两天,检查才知道是数据连接没关

以下是错误的:
Public Overloads Shared Sub delrecord(ByVal myid As Integer)
        Dim myconnection As New OleDbConnection(CommunityGlobals.ConnectionString)
        Dim mycommand As OleDbCommand = New OleDbCommand("delete from articles where id=" & myid, myconnection)
        myconnection.Open()
        mycommand.ExecuteNonQuery()
        ' myconnection.Close()
    End Sub
以下是正确的:
Public Overloads Shared Sub delrecord(ByVal myid As Integer)
        Dim myconnection As New OleDbConnection(CommunityGlobals.ConnectionString)
        Dim mycommand As OleDbCommand = New OleDbCommand("delete from articles where id=" & myid, myconnection)
        myconnection.Open()
        mycommand.ExecuteNonQuery()
         myconnection.Close()
    End Sub