【发布时间】: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?