每当我单击一个按钮以保存到Access数据库时,我都会不断收到错误消息:
参数没有默认值
尽管这些值已保存在数据库中。
我已经尝试在Access中设置一个默认值,但是我仍然遇到相同的异常。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each row As DataGridViewRow In DataGridView1.Rows
Dim constring As String = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\PU-IMO\Documents\Visual Studio 2012\Projects\InventoryMS\InventoryMS\Real.accdb")
Using con As New OleDbConnection(constring)
Using cmd As New OleDbCommand("INSERT INTO SalesInvoice([productID], [productnum], [productname], [productprice]) VALUES(productID, productnum, productname, productprice)", con)
cmd.Parameters.AddWithValue("@productID", row.Cells("productID").Value)
cmd.Parameters.AddWithValue("@productnum", row.Cells("productnum").Value)
cmd.Parameters.AddWithValue("@productname", row.Cells("productname").Value)
cmd.Parameters.AddWithValue("@productprice", row.Cells("productprice").Value)
con.Open()
Me.DataGridView1.ClearSelection()
Me.DataGridView1.SelectAll()
Me.DataGridView1.Rows(Me.DataGridView1.RowCount - 1).Selected = False
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
MessageBox.Show("Records inserted")
End Sub
答案 0 :(得分:0)
您的插入命令应为:-
"INSERT INTO SalesInvoice([productID], [productnum], [productname], [productprice])
VALUES(@productID, @productnum, @productname, @productprice)"
将@与值参数一起使用
答案 1 :(得分:0)
我想到的是我确实试图将一个空行保存到数据库中,因此它显示了上面的错误,现在可以通过一个简单的代码并通过@Charles May的帮助来解决该代码如下:
Private Sub Button1_Click(发送者为对象,e作为EventArgs)处理Button1.Click
For Each row As DataGridViewRow In DataGridView1.Rows
Dim constring As String = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\PU-IMO\Documents\Visual Studio 2012\Projects\InventoryMS\InventoryMS\Real.accdb")
Using con As New OleDbConnection(constring)
Using cmd As New OleDbCommand("INSERT INTO SalesInvoice([productID], [productnum], [productname], [productprice]) VALUES(@productID, @productnum, @productname, @productprice)", con)
cmd.Parameters.AddWithValue("@productID", row.Cells("productID").Value)
cmd.Parameters.AddWithValue("@productnum", row.Cells("productnum").Value)
cmd.Parameters.AddWithValue("@productname", row.Cells("productname").Value)
cmd.Parameters.AddWithValue("@productprice", row.Cells("productprice").Value)
DataGridView1.AllowUserToAddRows = False
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Next
MessageBox.Show("Records inserted")
End Sub