我有一个Access
数据库,并通过VB.NET
与其相关联。
首先,我在GridView
中显示一个表记录,用户可以更新它。用户更新后,他将按Button
从GridView
获取更新的数据并更新Access数据库。
但在这样做的过程中,我收到错误No value given for one or more required parameters.
我在这里失踪了什么?
代码:
Dim sConnectionString As String _
= "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\DB.accdb;Persist Security Info=False;"
Dim myConnection As OleDbConnection
Dim myAdapter As OleDbDataAdapter
Dim myTable As DataTable
Private Sub form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
myConnection = New OleDbConnection(sConnectionString)
Dim myCommand As New OleDbCommand("SELECT ID,Test FROM T1", myConnection)
myConnection.Open()
myAdapter = New OleDbDataAdapter(myCommand)
myTable = New DataTable()
myAdapter.Fill(myTable)
setDataGridDataSource(myTable)
setLabelTxt("Row Count: " + myTable.Rows.Count.ToString(), RowCount)
DataGridView1.AllowUserToAddRows = True
DataGridView1.AllowUserToDeleteRows = True
DataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
myConnection.Close()
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
Me.Validate()
Dim command As OleDbCommand = New OleDbCommand("UPDATE T1 SET ID = ? , Test = ? WHERE ID = ?;", myConnection)
myAdapter.UpdateCommand = command
myAdapter.Update(myTable)
myTable.AcceptChanges()
End Sub
答案 0 :(得分:0)
你的sql命令带有文字" UPDATE T1 SET ID =? ,测试=? WHERE ID =?;"需要3个参数,但您没有在代码中的任何位置设置它们。 See this example并为您的命令分配参数,如:
cmd.Parameters.AddWithValue("ID", txtID.Text);
cmd.Parameters.AddWithValue("Test", txtTest.Text);
... and so on
答案 1 :(得分:0)
代码应编辑为:
Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
Me.Validate()
Dim cmdBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(myAdapter)
Dim changes As DataTable = myTable.GetChanges
If changes IsNot Nothing Then
myAdapter.Update(myTable)
End If
myTable.AcceptChanges()
End Sub