将Datagrid数据保存到VB.Net中的SQL Server数据库 [英] Save Datagrid data to SQL Server database in VB.Net

查看:183
本文介绍了将Datagrid数据保存到VB.Net中的SQL Server数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagrid与数据。第一列是与进口商的组合框,而第二列是导入的数量。我想能够将数据保存到数据库,无论用户的选择如何。



请参阅下面的代码。有人可以帮我吗?

  Dim intReturnValue As Integer = 0 

Dim SqlCmd As String =更新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)

对于i = 0 To Me.dgvImporter.RowCount - 1
CmdObj.Parameters.AddWithValue(@ Importer,Me.dgvImporter.Rows(i)。单元格(0).Value)
CmdObj.Parameters.AddWithValue(@ Quantity,Me.dgvImporter.Rows(i).Cells(1).Value)
CmdObj.Parameters.AddWithValue(@产品,cboProductName.SelectedValue)
下一个

ConnObj.Open()

intReturnValue = CInt(CmdObj.ExecuteNonQuery())

ConnObj.Close()

如果intReturnValue> 0然后
MsgBox(您已成功更新产品表产品,MsgBoxStyle.Information,)
ClearForm()
Else
MsgBox(没有记录被插入,MsgBoxStyle.Exclamation,)
如果


解决方案

在循环中添加参数,但不执行命令,只有当您只有一行,并且如果必须更新多行时,才会导致错误。

在每个循环中继续读取相同的参数到相同的命令。



而是在循环之前移动参数的声明,并在循环内部设置它们的值,然后执行命令。

  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 )

对于i = 0 To Me.dgvImporter.RowCount - 1
CmdObj.Parameters(@ Importer)。Value = Convert.ToInt32(Me.dgvImporter.Rows(i))。单元格(0).Value)
CmdObj.Parameters(@ Quantity)。Value = Convert.ToInt32(Me.dgvImporter.Rows(i).Cells(1).Value)
CmdObj.Parameters (@Product)。Value = Convert.ToIn t32(cboProductName.SelectedValue)
intReturnValue = CInt(CmdObj.ExecuteNonQuery())

下一个

重要说明。 AddWithValue 发现查看传递值的参数的数据类型。这个假设是危险的,可能导致错误。最好使用专门的构造函数,您可以在其中定义参数


的类型和大小

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. 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

这篇关于将Datagrid数据保存到VB.Net中的SQL Server数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆