从数据库中删除datagridview中的数据

时间:2017-01-02 16:13:55

标签: mysql vb.net

下面是我用来从数据库中删除datagridview中的数据的代码,但是我遇到了错误:

enter image description here

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Try
        con.Open()
        For i As Integer = Me.DataGridView1.SelectedRows.Count - 1 To 0 Step -1
            CmdDelete = "DELETE rsdcpatienttbl WHERE TreatmentCount='" & Me.DataGridView1.SelectedRows(i).Cells(0).Value.ToString() & "'"
            adapter = New MySqlDataAdapter(CmdDelete, con)
            ds = New DataSet()
            adapter.Fill(ds)
            Me.DataGridView1.Rows.Remove(Me.DataGridView1.SelectedRows(i))

        Next
        load_table()

        MessageBox.Show("Deleted Successfully.", "Informed", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Catch ex As Exception

        MessageBox.Show(ex.ToString())
    End Try

End Sub

1 个答案:

答案 0 :(得分:0)

如果您使用MySqlDataAdapter执行操作,那么您不需要单独打开连接,删除con.Open()以跳过您获得的异常