vb.net DataGridView第二次删除出错,显示数据库连接关闭
代码如下:
Imports System.Windows.Forms
Imports System.Data.SqlClient
Public Class FrmSpec
Inherits System.Windows.Forms.Form
Private Cn As SqlConnection
Private Da As SqlDataAdapter
Private Cmd As SqlCommand
Private Ds As DataSet
Private A As Integer
Private Sub FrmSpec_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Closed
FrmMain.Enabled = True
FrmMain.Focus()
End Sub
Private Sub CheckCn()
End Sub
Private Sub FrmSpec_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Cn = New SqlConnection(cnStr)
If (Cn.State = ConnectionState.Closed) Then
Cn.Open()
End If
Sql = "select prdcom x1,x2,x3,x4 from pr1 "
Da = New SqlDataAdapter(Sql, Cn)
Ds = New DataSet()
Da.Fill(Ds, "prd")
Dim Bs As New BindingSource
Bs.DataSource = Ds.Tables("prd")
BdNtor1.BindingSource = Bs
DGView1.DataSource = Bs
With DGView1
.Columns(0).Width = 80
.Columns(1).Width = 70
.Columns(2).Width = 120
.Columns(3).Width = 130
End With
DGView1.Refresh()
Cn.Close()
End Sub
Private Sub DGView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGView1.CellClick
A = e.RowIndex
End Sub
Private Sub DelRec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DelRec.Click
If MessageBox.Show("你确定要删除这一笔资料?", "删除资料", MessageBoxButtons.OKCancel) = DialogResult.OK Then
Cn = New SqlConnection(cnStr)
If (Cn.State = ConnectionState.Closed) Then
Cn.Open()
End If
Sql = "DELETE FROM pr1 WHERE prdcode='" & DGView1.Rows(A).Cells(0).Value.ToString & "'"
Static Cmd As New SqlCommand(Sql, Cn)
Try
Cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Cn.Close()
End If
Call FrmSpec_Load(Nothing, Nothing)
End Sub
End Class
第一次删除正常,关闭后再删除也正常,但连续删除第二次提示,当前连接已关闭
请高手帮忙看下