如何从连接到数据库的组合框中删除项目并自动刷新它?

How to delete items from combo box which is connected to database and automatically refresh it?

我有点小麻烦。 我在这里想做的是删除连接到数据库的组合框中的一个项目,删除后,它会自动刷新组合框的内容。

        Dim cons As OleDb.OleDbConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\athan\Documents\PAGEANT.accdb")

        Using cons


            With cmd
                .Connection = cons
                .CommandText = "DELETE FROM  Judges Where Fullname = @FullName"
                .Parameters.AddWithValue("@FullName", cmbJudges.SelectedItem.ToString)
                .Connection.Open()
                .ExecuteNonQuery()
                .Connection.Close()

                MessageBox.Show("RECORD HAS BEEN DELETED", "DELETE", MessageBoxButtons.OK, MessageBoxIcon.Information)


                txtFullNameJudge.Text = Nothing
                txtContactJudge.Text = Nothing
                txtUserNameJudge.Text = Nothing
                txtPasswordJudge.Text = Nothing
                cmbJudges.Text = ""

            End With
        End Using

它正在删除一条记录,但它没有刷新组合框的内容,所以我无法删除其他数据。好像只能删除一个数据。然后我需要重新运行程序以便删除生效。

第一种方法代码较多,但我认为第二种方法更接近@jmcilhinney 在评论中建议的方法,可能会带来问题。我认为 DataAdapter 需要一个主键才能成为 Select 的一部分。如果 FullName 不是 Judges table 的主键,则此方法可能无效。

您可能希望使 dtDatatable)成为表单级变量,这样您就不必从 .DataSource 属性 中提取和转换它.然后它将可用于您填充组合的方法和删除法官的方法。如果这样做,请务必删除局部变量。

Private Sub FillJudgesCombo()
    Dim dt As New DataTable
    Using cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\athan\Documents\PAGEANT.accdb"),
            cmd As New OleDbCommand("Select FullName From Judges")
        cn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    cmbJudges.DisplayMember = "FullName"
    cmbJudges.DataSource = dt
End Sub

Private Sub DeleteJudge1()
    'I stored this in a local variable because we are using it more tha once in this method
    Dim NameToDelete = cmbJudges.Text
    'To update the database
    Using cons As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\athan\Documents\PAGEANT.accdb"),
            cmd As New OleDbCommand("DELETE FROM  Judges Where Fullname = @FullName", cons)
        'had to guess at the datatype and field size - check your database
        'The Add method is much preferred to the .AddWithValue
        cmd.Parameters.Add("@FullName", OleDbType.VarChar, 200).Value = NameToDelete
        cons.Open()
        cmd.ExecuteNonQuery()
    End Using
    'To update the combo box by editing DataTable
    Dim dt = DirectCast(ComboBox1.DataSource, DataTable)
    'This line uses and interpolated string indicated by the $ preceding the string.
    'This allows to insearch a variable in place in the string surronded by braces.
    Dim dr = dt.Select($"FullName = {NameToDelete}")
    dr(0).Delete()
    dt.AcceptChanges()
    ResetTextBoxesAndCombo()
    MessageBox.Show("RECORD HAS BEEN DELETED", "DELETE", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

Private Sub ResetTextBoxesAndCombo()
    'Offloaded this because it really has nothing to do with deleting a judge.
    txtFullNameJudge.Text = Nothing
    txtContactJudge.Text = Nothing
    txtUserNameJudge.Text = Nothing
    txtPasswordJudge.Text = Nothing
    cmbJudges.SelectedIndex = -1
End Sub

Private Sub DeleteJudge2()
    Dim NameToDelete = cmdJudges.Text
    Dim dt = DirectCast(ComboBox1.DataSource, DataTable)
    'The Select method has no idea how many rows are returned
    'so it returns an array of DataRows
    Dim dr = dt.Select($"FullName = {NameToDelete}")
    'We are only interested in the first item in the array.
    'The array should only have one element.
    dr(0).Delete()
    Using con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\athan\Documents\PAGEANT.accdb"),
            da As New OleDbDataAdapter("Select FullName From PAGEANT", con)
        Dim cmdBuild As New OleDbCommandBuilder(da)
        da.Update(dt)
    End Using
    ResetTextBoxesAndCombo()
    MessageBox.Show("RECORD HAS BEEN DELETED", "DELETE", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub

它现在像我想要的那样工作。每当我通过组合框删除数据时,它都会删除它,然后使用 FullName 字段中的值重新加载组合框。 @Mary 和@jmcilhinney 感谢所有帮助。

这是我的删除按钮的工作代码:

 Private Sub btnJudgeDelete_Click(sender As Object, e As EventArgs) Handles btnJudgeDelete.Click


        Using cons As New OleDb.OleDbConnection(conSTR)
            Using com As New OleDb.OleDbCommand("DELETE FROM Judges WHERE FullName = @FullName")
                com.Parameters.AddWithValue("@FullName", cmbJudges.Text)
                com.Connection = cons
                cons.Open()
                com.ExecuteNonQuery()

                MessageBox.Show("DATA HAS BEEN DELETED")
            End Using
        End Using


        cmbJudges.Text = ""
        txtFullNameJudge.Text = Nothing
        txtContactJudge.Text = Nothing
        txtUserNameJudge.Text = Nothing
        txtPasswordJudge.Text = Nothing
        cmbJudges.Items.Clear()

        Dim rd As OleDb.OleDbDataReader
        Using cn As New OleDb.OleDbConnection(conSTR),
            cmd As New OleDb.OleDbCommand("Select FullName From Judges", cn)
            cn.Open()
            rd = cmd.ExecuteReader
            While rd.Read
                cmbJudges.Items.Add(rd.Item(0))
            End While
            rd.Close()
        End Using



    End Sub