1
我有一个数据的数据网格。第一列是与进口商的组合框,第二列是进口数量。我希望能够将这些数据保存到数据库,而不管用户的选择。保存Datagrid数据到VB.Net中的SQL Server数据库
请看我的代码如下。有人可以帮我吗?
Dim intReturnValue As Integer = 0
Dim SqlCmd As String = "Update Importer_Balance SET Quantity = Quantity + @Quantity WHERE [email protected] and [email protected]"
Dim ConnObj As SqlConnection = New SqlConnection(clsGlobals.ConnString)
Dim CmdObj As SqlCommand = New SqlCommand(SqlCmd, ConnObj)
For i = 0 To Me.dgvImporter.RowCount - 1
CmdObj.Parameters.AddWithValue("@Importer", Me.dgvImporter.Rows(i).Cells(0).Value)
CmdObj.Parameters.AddWithValue("@Quantity", Me.dgvImporter.Rows(i).Cells(1).Value)
CmdObj.Parameters.AddWithValue("@Product", cboProductName.SelectedValue)
Next
ConnObj.Open()
intReturnValue = CInt(CmdObj.ExecuteNonQuery())
ConnObj.Close()
If intReturnValue > 0 Then
MsgBox("You have successfully updated the product table product.", MsgBoxStyle.Information, "")
ClearForm()
Else
MsgBox("No Record were inserted", MsgBoxStyle.Exclamation, "")
End If
Steve。感谢您的回应。审查我的代码并按照你的建议后,我得到这个错误:无法投入'System.Int32'类型的对象键入'System.Data.SqlClient.SqlParameter'。 – 2014-10-04 14:11:57
我的不好,缺少最后一个参数中的值 – Steve 2014-10-04 14:12:42
在您的回复之前,我将整个代码放在循环中。我得到的错误与我在遵循该指示后收到的错误类似。错误是:参数化查询'(@Importer nvarchar(4000),@ Quantity nvarchar(4000),@ Product int)'需要参数'@Importer',该参数未提供。 – 2014-10-04 14:19:23