我的程序无法更新其数据。代码已由我的讲师检查过。当我点击更新按钮时,数据没有变化。这与代码没有冲突。该程序只是没有响应更新查询。
Imports System.Data.OleDb
Public Class frmUpdate
Dim con1 As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source= MainSource.accdb")
Private Sub btnUp_Click(sender As Object, e As EventArgs) Handles btnUp.Click
If txtID.Text = "" Or txtItem.Text = "" Or ComboBox1.Text = "" Or txtPrice.Text = "" Or txtStock.Text = "" Or TextBox1.Text = "" Or TextBox2.Text = "" Then
MessageBox.Show("Please complete the required fields..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
Dim sqlupdate As String
sqlupdate = "UPDATE Inventory SET Item_Name=@Item_Name, Category=@Category, Stock_Quantity=@Stock_Quantity, Item_Price=@Item_Price, Supplier_ID=@Supplier_ID, Supplier_Name=@Supplier_Name WHERE Item_ID=@Item_ID"
Dim cmd As New OleDbCommand(sqlupdate, con1)
cmd.Parameters.Add(New OleDbParameter("@Item_ID", txtID.Text))
cmd.Parameters.Add(New OleDbParameter("@Item_Name", txtItem.Text))
cmd.Parameters.Add(New OleDbParameter("@Category", ComboBox1.Text))
cmd.Parameters.Add(New OleDbParameter("@Stock_Quantity", txtStock.Text))
cmd.Parameters.Add(New OleDbParameter("@Item_Price", txtPrice.Text))
cmd.Parameters.Add(New OleDbParameter("@Supplier_ID", TextBox1.Text))
cmd.Parameters.Add(New OleDbParameter("@Supplier_Name", TextBox2.Text))
con1.Open()
cmd.ExecuteNonQuery()
con1.Close()
ClearTextBox(Me)
Me.Close()
RefreshDGV()
End If
End Sub
如果你看到它有什么问题,那就说吧。我只想了解问题的根源。数据库放在名为 MainSource 的调试文件夹中。
由于
编辑:我使用DataGridView显示数据库的内容。一个表的一个DataGridView。我在数据库中有4个表。每个表都有一个单独的DataGridView。
Private Sub frmMain_Load(sender as Object,e As EventArgs)处理MyBase.Load Dim sql As String sql =" SELECT * FROM INVENTORY" Dim adapter As New OleDbDataAdapter(sql,con1) Dim dt As New DataTable(" INVENTORY") adapter.Fill(DT) DataGridView1.DataSource = dt Dim sql1 As String sql1 =" SELECT * FROM INVENTORY" Dim adapter1 As New OleDbDataAdapter(sql1,con1) Dim cmd1 As New OleDbCommand(sql1,con1) Dim dt1 As New DataTable(" INVENTORY")
Dim sql2 As String
sql2 = "SELECT * FROM Supplier"
Dim adapter2 As New OleDbDataAdapter(sql2, con1)
Dim dt2 As New DataTable("Supplier")
adapter2.Fill(dt2)
DataGridView9.DataSource = dt2
Dim sql3 As String
sql3 = "SELECT * FROM Supplier"
Dim adapter3 As New OleDbDataAdapter(sql3, con1)
Dim cmd3 As New OleDbCommand(sql3, con1)
Dim dt3 As New DataTable("Supplier")
con1.Open()
Dim myreader As OleDbDataReader = cmd1.ExecuteReader
myreader.Read()
Dim myreader2 As OleDbDataReader = cmd3.ExecuteReader
myreader2.Read()
con1.Close()
Dim sql4 As String
sql4 = "SELECT * FROM Purchase"
Dim adapter4 As New OleDbDataAdapter(sql4, con1)
Dim dt4 As New DataTable("Purchase")
adapter4.Fill(dt4)
DataGridView2.DataSource = dt4
Dim sql5 As String
sql5 = "SELECT * FROM Purchase"
Dim adapter5 As New OleDbDataAdapter(sql5, con1)
Dim cmd5 As New OleDbCommand(sql5, con1)
Dim dt5 As New DataTable("Purchase")
Dim sql6 As String
sql6 = "SELECT * FROM Restock"
Dim adapter6 As New OleDbDataAdapter(sql6, con1)
Dim dt6 As New DataTable("Restock")
adapter6.Fill(dt6)
DataGridView6.DataSource = dt6
Dim sql7 As String
sql7 = "SELECT * FROM Restock"
Dim adapter7 As New OleDbDataAdapter(sql7, con1)
Dim cmd7 As New OleDbCommand(sql7, con1)
Dim dt7 As New DataTable("Restock")
con1.Open()
Dim myreader6 As OleDbDataReader = cmd7.ExecuteReader
myreader6.Read()
Dim myreader5 As OleDbDataReader = cmd5.ExecuteReader
myreader5.Read()
con1.Close()
Dim sql14 As String
sql14 = "SELECT * FROM Supplier"
Dim adapter14 As New OleDbDataAdapter(sql14, con1)
Dim dt14 As New DataTable("Supplier")
adapter14.Fill(dt14)
DataGridView9.DataSource = dt14
Dim sql15 As String
sql15 = "SELECT * FROM Supplier"
Dim adapter15 As New OleDbDataAdapter(sql15, con1)
Dim cmd15 As New OleDbCommand(sql15, con1)
Dim dt15 As New DataTable("Supplier")
Dim myreader8 As OleDbDataReader = cmd15.ExecuteReader
myreader8.Read()
con1.Close()
Me.ReportViewer1.RefreshReport()
End Sub
这是来自主窗口。希望它有所帮助。