You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何更新数据库中已编辑的ComboBox项而非新增?

ComboBox编辑后更新数据库的问题

现状与问题

我有一个名为Barcode的ComboBox,通过以下代码从数据库填充:

Sub fillIBarcode()
    Barcode.DataSource = Nothing
    Barcode.Items.Clear()
    Dim adp As New SqlClient.SqlDataAdapter("Select distinct Barcode from Items where  ItemCode=N'" & (ItemsDGV.CurrentRow.Cells(0).Value) & "'", SQlconn)
    Dim ds As New DataSet
    adp.Fill(ds)
    Dim dt = ds.Tables(0)
    '=====================================================
    For I = 0 To dt.Rows.Count - 1
        Barcode.Items.Add(dt.Rows(I).Item("Barcode"))
        Barcode.SelectedIndex = 0
    Next
End Sub

ComboBox会显示对应商品的多个条码选项。当用户编辑ComboBox中的内容并点击Edit按钮时,需要提交修改并立即更新数据库中的ComboBox项列表,但尝试的代码会新增值而非更新原有值——原本两个条码加上编辑后的新条码,最终变成三个。

尝试的Edit按钮代码:

Private Sub Editbtn_Click(sender As Object, e As EventArgs) Handles Editbtn.Click
    Dim cmdd As New SqlClient.SqlCommand
    cmdd.Connection = SQlconn
    cmdd.CommandText = "delete from Items where ItemCode=N'" & (ItemCode.Text) & "'"
    cmdd.ExecuteNonQuery()

    Dim sql = "select * From Items where ItemCode=N'" & (ItemCode.Text) & "'"
    Dim adp As New SqlClient.SqlDataAdapter(sql, SQlconn)
    Dim ds As New DataSet
    adp.Fill(ds)
    Dim dt = ds.Tables(0)
    If dt.Rows.Count = 0 Then               
        For i = 0 To Barcode.Items.Count - 1
            Dim dr = dt.NewRow
            dr!ItemCode = ItemCode.Text
            dr!Barcode = Barcode.Items(i).ToString
            dt.Rows.Add(dr)
        Next
        Dim cmd As New SqlClient.SqlCommandBuilder(adp)
        adp.Update(dt)
    End If
End Sub

问题根源与修正方案

你的代码逻辑是先删除该商品所有条码,再重新插入ComboBox里的所有项,但编辑后你没有移除旧的条码项,反而把编辑后的新条码和原有旧条码一起插入,导致数量叠加。另外,全删全插的方式不够严谨,应该针对具体条码做精准更新。

修正后的Edit按钮代码

Private Sub Editbtn_Click(sender As Object, e As EventArgs) Handles Editbtn.Click
    ' 校验:确保有选中项且内容确实被修改
    If Barcode.SelectedIndex = -1 OrElse Barcode.Text.Trim() = Barcode.SelectedItem.ToString().Trim() Then
        MessageBox.Show("请修改条码内容后再提交")
        Return
    End If

    Dim oldBarcode As String = Barcode.SelectedItem.ToString().Trim()
    Dim newBarcode As String = Barcode.Text.Trim()
    Dim itemCode As String = ItemCode.Text.Trim()

    ' 使用参数化查询,避免SQL注入,同时处理编码问题
    Using updateCmd As New SqlClient.SqlCommand(
        "UPDATE Items SET Barcode = @NewBarcode WHERE ItemCode = @ItemCode AND Barcode = @OldBarcode", 
        SQlconn)
        
        updateCmd.Parameters.AddWithValue("@NewBarcode", newBarcode)
        updateCmd.Parameters.AddWithValue("@ItemCode", itemCode)
        updateCmd.Parameters.AddWithValue("@OldBarcode", oldBarcode)

        Try
            Dim affectedRows As Integer = updateCmd.ExecuteNonQuery()
            If affectedRows > 0 Then
                ' 刷新ComboBox数据源,同步数据库变更
                fillIBarcode()
                ' 自动选中修改后的条码
                Dim targetIndex As Integer = Barcode.FindStringExact(newBarcode)
                If targetIndex <> -1 Then
                    Barcode.SelectedIndex = targetIndex
                End If
                MessageBox.Show("条码更新成功")
            Else
                MessageBox.Show("未找到对应条码,更新失败")
            End If
        Catch ex As Exception
            MessageBox.Show("更新出错:" & ex.Message)
        End Try
    End Using
End Sub

优化原填充代码

原fillIBarcode()可以简化为直接绑定数据源,更高效且避免循环冗余:

Sub fillIBarcode()
    Barcode.Items.Clear()
    ' 同样使用参数化查询
    Dim adp As New SqlClient.SqlDataAdapter(
        "SELECT DISTINCT Barcode FROM Items WHERE ItemCode = @ItemCode", 
        SQlconn)
    adp.Parameters.AddWithValue("@ItemCode", ItemsDGV.CurrentRow.Cells(0).Value)
    
    Dim dt As New DataTable()
    adp.Fill(dt)

    ' 直接绑定数据源
    Barcode.DataSource = dt
    Barcode.DisplayMember = "Barcode"
    Barcode.ValueMember = "Barcode"
    
    If dt.Rows.Count > 0 Then
        Barcode.SelectedIndex = 0
    End If
End Sub

关键注意事项

  • 必须使用参数化查询,避免SQL注入风险,同时解决字符串拼接带来的编码、特殊字符问题
  • 不要采用全删全插的方式更新,精准定位要修改的条码记录,避免误删其他数据
  • 操作数据库后务必调用fillIBarcode()刷新ComboBox,保证界面与数据库同步

内容的提问来源于stack exchange,提问作者Point_Systems

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 00:25:26