2012-10-30 116 views
2

我有我的代码在这里没有错误(至少不是当我调试它,我使用VS 2010),但我希望发生的是当我点击添加按钮,数字/秒在文本框(txtQty)中将添加到当前保存在“数量”列中的数字。 (例如:txtQty“100”,当前在列上是“200”,我想从文本框添加“100”到具有“200”的列并将其保存为新的数字“300”。 ,但问题是我还没有知道如何使用数学公式VB.NETDatagridviewMySql Database此代码执行,但当我点击添加列中的数字返回到0.任何提示或技巧将不胜感激,谢谢。VB.NET和MySql更新查询

 cmd.Connection = conn 

     Dim Result = "Quantity" + txtQty.Text 

     cmd.CommandText = " UPDATE incomingdeliveries SET Quantity ='" & Result & "';" 
     cmd.Parameters.Add(New MySqlParameter("@Quantity", txtQty.Text)) 

     cmd.ExecuteNonQuery() 

     MsgBox("Data Added to the Database") 

      Me.IncomingdeliveriesTableAdapter.Dispose() 
     Me.IncomingdeliveriesTableAdapter.Fill(Me.IncomingDeliveriesDataSet.incomingdeliveries) 

      lblCode.Text = ("Tables Refreshed!") 

回答

3

命令文本也必须参数

cmd.CommandText = " UPDATE incomingdeliveries SET Quantity = @iQuantity + Quantity" 
cmd.Parameters.Add(New MySqlParameter("@iQuantity", txtQty.Text)) 

更新1

Using _conn As New MySqlConnection("connectionStr here") 
    Using _comm As New MySqlCommand() 
     With _comm 
      .Connection = _conn 
      .CommandText = "UPDATE incomingdeliveries SET Quantity = @iQuantity + Quantity" 
      .CommandType = CommandType.Text 
      .Parameters.AddWithValue("@iQuantity", txtQty.Text) 
     End With 
     Try 
      _conn.Open() 
      _comm.ExecuteNonQuery() 
     Catch ex As MySqlException 
      Msgbox(ex.Message.ToString()) 
     End Try 
    End Using 
End Using 
+0

ok我改变了我的查询到你的感谢,现在它能够添加,但现在的问题是,整个列正在改变。 (例如:我加100,现在所有的列都添加了100),同样当我再次单击添加时,我得到这个错误(**参数'@iQuantity'已经被定义。**) –

+0

是'cmd'全局声明? *坏*。但是你可以在命令文本前添加'cmd.Parameters.Clear()'。 –

+0

@AlexLuthor之一,你应该在你的更新查询中使用where子句指定要更新的行(可能带有一个id?)。二,你应该使用'var cmd = new()'而不是总是使用相同的SqlCommand字段。换句话说,每次都实例化一个新实例,最好使用'using'子句。我希望约翰能修改他的答案以反映这一点。 – nawfal