从 Microsoft Access 数据库更新组合框中的项目

Posted

技术标签:

【中文标题】从 Microsoft Access 数据库更新组合框中的项目【英文标题】:Updating items in ComboBoxes from an Microsoft Access database 【发布时间】:2021-03-25 15:57:23 【问题描述】:

我正在VB.NET 中制作家具租赁系统,以从Microsoft Access 数据库更新ComboBox。此更新ComboBox 的代码不起作用;它仅适用于一个项目,对于其他项目,它显示“未找到记录”。

我正在为我的数据库使用Microsoft Access

Public Class Form8
    Dim cn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\jeeva\Desktop\VB Project\18HU5A1015.accdb")
    Private Sub update_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cn.Open()
        Dim cm As New OleDb.OleDbCommand("select * from customerinfo", cn)
        Dim dr As OleDb.OleDbDataReader = cm.ExecuteReader
        While dr.Read
            ComboBox1.Items.Add(dr(0).ToString)
            ComboBox2.Items.Add(dr(1).ToString)
            ComboBox3.Items.Add(dr(2).ToString)
            ComboBox4.Items.Add(dr(3).ToString)
        End While
        dr.Close()
        cn.Close()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim customername = TextBox1.Text
        Dim customerid = TextBox2.Text
        Dim customeraddress = TextBox3.Text
        Dim customeraadharno = TextBox4.Text
        Try
            cn.Open()
            Dim cmd As New OleDb.OleDbCommand()
            cmd.CommandText = "Update customerinfo set customername='" + customername + "' where customername='" + ComboBox1.SelectedItem() + "'"
            cmd.CommandText = "Update customerinfo set customerid='" + customerid + "' where customername='" + ComboBox2.SelectedItem() + "'"
            cmd.CommandText = "Update customerinfo set customeraddress='" + customeraddress + "' where customername='" + ComboBox3.SelectedItem() + "'"
            cmd.CommandText = "Update customerinfo set customeraadharno='" + customeraadharno + "' where customername='" + ComboBox4.SelectedItem() + "'"
            cmd.Connection = cn

            Dim i = cmd.ExecuteNonQuery
            If i > 0 Then
                MsgBox("Record is updated successfully")
            Else
                MsgBox("No record found")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            cn.Close()
        End Try
    End Sub

    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click

    End Sub

【问题讨论】:

你不断替换cmd.CommandText。只有最后一个被调用。使用参数避免sql注入和格式错误。 你能告诉我怎么做吗?其实我是新手 您的代码有点奇怪,尤其是您的四个 ComboBox。不确定它们与您的查询中的customername 有何关系。 Google sql 更新表中的多个列。 Customername 是 MS Access 中的一张表,里面有 customername、customerid、customeraddress 和 customeraadharno 作为字段。 但是在您的四个查询中,您调用了where customername='" + ComboBox1.SelectedItem() + "'" 和 ComboBox2 等。为什么当您的负载中的四个不同列填充四个 ComboBox 时,它们都包含一个 CustomerName?避免使用SELECT * 并指定实际列:SELECT ColumnName1, ColumnName2, etc 另请参阅How can I add user-supplied input to an SQL statement? 【参考方案1】:

不要更改 customerid,您需要使用唯一的 customerid 来更新表。 看看下面的例子:

Dim cn As New OleDb.OleDbConnection("Provider=...;")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    cn.Open()
    Dim adapter As New OleDb.OleDbDataAdapter("select customerid,customername from customerinfo", cn)
    Dim dt As DataTable = New DataTable
    adapter.Fill(dt)
    ComboBox1.DataSource = dt
    ComboBox1.DisplayMember = "customername"
    ComboBox1.ValueMember = "customerid"
    cn.Close()

End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try

        Dim cmd As New OleDb.OleDbCommand()
        cmd.Connection = cn

        cmd.CommandText = "Update customerinfo set customername = @customername, customeraddress = @customeraddress, customeraadharno = @aadharno where customerid=@customerid"
        cmd.Parameters.AddWithValue("customername", TextBox1.Text)
        cmd.Parameters.AddWithValue("customeraddress", TextBox2.Text)
        cmd.Parameters.AddWithValue("aadharno", TextBox3.Text)
        cmd.Parameters.AddWithValue("customerid", ComboBox1.SelectedValue)
        cn.Open()
        cmd.ExecuteNonQuery()
        MsgBox("Successfully update customerinfo")
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        cn.Close()
    End Try
End Sub

【讨论】:

以上是关于从 Microsoft Access 数据库更新组合框中的项目的主要内容,如果未能解决你的问题,请参考以下文章

从 XCode 连接到 Microsoft Access 数据库

java.sql.SQLException:[Microsoft][ODBC Microsoft Access Driver] 无法更新。数据库或对象是只读的

使用传递查询中的数据更新 Microsoft Access 2013 表

Microsoft Access 更新查询

如何使用空字符串更新 Microsoft Access 数据库?

在使用SQL Server后端的Microsoft Access中使用查找对话框时性能下降