简体   繁体   English

将Datagrid数据保存到VB.Net中的SQL Server数据库中

[英]Save Datagrid data to SQL Server database in VB.Net

I have a datagrid with data. 我有一个数据网格。 Column one is a combobox with importers whereas column two is the quantity imported. 第一栏是带有进口商的组合框,而第二栏是进口数量。 I want to be able to save this data to the DB irrespective of the user's selection. 我希望能够将此数据保存到数据库,而不考虑用户的选择。

Please see my code show below. 请在下面查看我的代码。 Can someone help me out? 有人可以帮我吗?

Dim intReturnValue As Integer = 0

Dim SqlCmd As String = "Update Importer_Balance SET Quantity = Quantity + @Quantity WHERE Importer=@Importer and Product=@product" 

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

Inside the loop you add the parameters but don't execute the command, this will work only if you have just one row and results in an error if you have to update more than one row. 在循环内添加参数但不执行命令,这仅在只有一行的情况下有效,而在必须更新多行的情况下会导致错误。
You continue to readd the same parameters at each loop to the same command. 在每个循环中,您继续读取同一命令的相同参数。

Instead move the declaration of the parameters before the loop and inside the loop just set their values and then execute the command. 而是将参数的声明移到循环之前和循环内部,只需设置它们的值,然后执行命令即可。

Dim intReturnValue As Integer = 0
Dim SqlCmd As String = "Update Importer_Balance SET Quantity = Quantity + @Quantity WHERE Importer=@Importer and Product=@product" 

Dim ConnObj As SqlConnection = New SqlConnection(clsGlobals.ConnString)
Dim CmdObj As SqlCommand = New SqlCommand(SqlCmd, ConnObj)
CmdObj.Parameters.Add("@Importer", SqlDbType.Int)
CmdObj.Parameters.Add("@Quantity", SqlDbType.Int)
CmdObj.Parameters.Add("@Product", SqlDbType.Int)
ConnObj.Open()

For i = 0 To Me.dgvImporter.RowCount - 1
    CmdObj.Parameters("@Importer").Value = Convert.ToInt32(Me.dgvImporter.Rows(i).Cells(0).Value)
    CmdObj.Parameters("@Quantity").Value = Convert.ToInt32(Me.dgvImporter.Rows(i).Cells(1).Value)
    CmdObj.Parameters("@Product").Value = Convert.ToInt32(cboProductName.SelectedValue)
    intReturnValue = CInt(CmdObj.ExecuteNonQuery())

Next

Important note. 重要的提示。 AddWithValue discovers the datatype of the parameter looking at the value passed. AddWithValue通过查看传递的值AddWithValue发现参数的数据类型。 This assumption is dangerous and could lead to errors. 这种假设很危险,并可能导致错误。 It is better to use the specialized constructor where you can define the type and the size of the parameter 最好使用专门的构造函数,您可以在其中定义参数的类型和大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM