我在DataGridView控件中有多行数据,我想将每一行插入到数据库中。我尝试了以下方法,但它给出了错误消息:"参数已添加"。如何仅添加一次参数名称,然后每次添加值并执行?
Using connection As New SqlCeConnection(My.Settings.databaseConnectionString)
Using command As New SqlCeCommand("INSERT INTO table_master(item, price) VALUES(@item, @price)", _
connection)
connection.Open()
For Each r As DataGridViewRow In dgvMain.Rows
If (Not String.IsNullOrWhiteSpace(r.Cells(1).Value)) Then
command.Parameters.AddWithValue("@item", r.Cells(1).Value.Trim)
command.Parameters.AddWithValue("@price", r.Cells(2).Value)
command.ExecuteNonQuery()
End If
Next
End Using
End Using