none
想请指点有关user从combobox 删除access 的data RRS feed

  • 问题

  • 以下代码是user从combobox 删除access database的代码,我尝试了几次还是删除数据失败想请指教有关combobox 删除access 的data的代码

    Private tbl_Category As New System.Data.DataTable("Category")

        Private Sub DC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DC.Click
            Const _ConnectionString As String = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=FA_Audit_Asset_Det.mdb;"

            If txtCATEGORY.SelectedItem IsNot Nothing AndAlso TypeOf (txtCATEGORY.SelectedItem) Is System.Data.DataRowView Then
                Using conn As New System.Data.OleDb.OleDbConnection(_ConnectionString)
                    conn.Open()
                    Dim row As System.Data.DataRowView = DirectCast(txtCATEGORY.SelectedItem, System.Data.DataRowView)
                    Dim sqlcmd As New System.Data.OleDb.OleDbCommand(String.Format("delete * from tbl_Category where id={0}", row.Item(txtCATEGORY.Text)), conn)
                    If sqlcmd.ExecuteNonQuery() = 1 Then
                        row.Delete()
                        tbl_Category.AcceptChanges()
                    End If
                End Using
            End If
        End Sub

    2020年6月3日 7:02

答案

  • Hi christing,

    根据你的问题,我做了一个测试,你可以参考以下测试代码来从数据库中通过选择的 combobox 的值删除数据。

        Dim cnn As OleDbConnection = New OleDbConnection()
        Dim cmd As New OleDb.OleDbCommand
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            cnn.ConnectionString = "your connection string"
            cnn.Open()
            Dim cmdtext As String = "SELECT ID FROM Table1 ORDER BY ID"
            Using cmd = New OleDbCommand(cmdtext, cnn)
                Dim reader = cmd.ExecuteReader()
                While reader.Read()
                    ComboBox1.Items.Add(reader("ID"))
                End While
            End Using
    
        End Sub
        Private Sub DeleteBtn_Click(sender As Object, e As EventArgs) Handles DeleteBtn.Click
            If ComboBox1.SelectedItem Is Nothing Then
                Return
            Else
                If Not cnn.State = ConnectionState.Open Then
                    cnn.Open()
                End If
                cmd.Parameters.Clear()
                Using cmd = New OleDbCommand()
                    cmd.Connection = cnn
                    cmd.CommandText = "DELETE FROM Table1 WHERE ID = @id;"
                    cmd.Parameters.AddWithValue("@id", ComboBox1.SelectedItem)
                    cmd.ExecuteNonQuery()
                End Using
                MessageBox.Show("Record Is Deleted")
                ComboBox1.Items.Remove(ComboBox1.SelectedItem)
                ComboBox1.Text = ""
            End If
        End Sub

    希望可以帮助你解决问题。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2020年6月4日 7:04
    版主

全部回复

  • Hi christing,

    为了更好的分析你的问题和进行测试,我有以下问题要和你确认一下。

    是否你的 combobox 保存有数据库中 'tbl_Category' 的所有 id 值, 当你选择 combobox 中的一个值时,点击删除按钮你可以将相对应的 id 从数据库中的表中删除?

    txtCATEGORY 是否就是你的 combobox呢?

    期待你的更新。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年6月4日 2:29
    版主
  • HI XING YU ZHAO

    感谢你的回复

    txtCATEGORY 是否就是你的 combobox呢?

    是的

    是否你的 combobox 保存有数据库中 'tbl_Category' 的所有 id 值, 当你选择 combobox 中的一个值时,点击删除按钮你可以将相对应的 id 从数据库中的表中删除?

    是的

    下图是我的tbl_Category

    2020年6月4日 3:33
  • Hi christing,

    根据你的问题,我做了一个测试,你可以参考以下测试代码来从数据库中通过选择的 combobox 的值删除数据。

        Dim cnn As OleDbConnection = New OleDbConnection()
        Dim cmd As New OleDb.OleDbCommand
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            cnn.ConnectionString = "your connection string"
            cnn.Open()
            Dim cmdtext As String = "SELECT ID FROM Table1 ORDER BY ID"
            Using cmd = New OleDbCommand(cmdtext, cnn)
                Dim reader = cmd.ExecuteReader()
                While reader.Read()
                    ComboBox1.Items.Add(reader("ID"))
                End While
            End Using
    
        End Sub
        Private Sub DeleteBtn_Click(sender As Object, e As EventArgs) Handles DeleteBtn.Click
            If ComboBox1.SelectedItem Is Nothing Then
                Return
            Else
                If Not cnn.State = ConnectionState.Open Then
                    cnn.Open()
                End If
                cmd.Parameters.Clear()
                Using cmd = New OleDbCommand()
                    cmd.Connection = cnn
                    cmd.CommandText = "DELETE FROM Table1 WHERE ID = @id;"
                    cmd.Parameters.AddWithValue("@id", ComboBox1.SelectedItem)
                    cmd.ExecuteNonQuery()
                End Using
                MessageBox.Show("Record Is Deleted")
                ComboBox1.Items.Remove(ComboBox1.SelectedItem)
                ComboBox1.Text = ""
            End If
        End Sub

    希望可以帮助你解决问题。

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    2020年6月4日 7:04
    版主