【问题标题】:VB.NET and MySql UPDATE queryVB.NET 和 MySql 更新查询
【发布时间】:2012-10-19 10:37:35
【问题描述】:

我的代码在这里没有错误(至少在我调试它时没有,我使用VS 2010),但我希望发生的是当我点击添加按钮时,文本框中的数字(txtQty ) 将添加到当前保存在“数量”列中的数字。 (例如:txtQty“100”,当前列是“200”,我想将“100”从文本框中添加到“200”列并将其保存为新数字“300”。我知道的简单数学,但问题是我还不知道如何在VB.NET DatagridviewMySql Database 上使用数学方程式此代码执行但是当我单击添加列中的数字时返回0。任何提示或提示都会很多感谢,谢谢。

        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!")

【问题讨论】:

    标签: mysql vb.net winforms visual-studio-2010 datagridview


    【解决方案1】:

    您的命令文本也必须参数化

    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
    

    【讨论】:

    • 好的,我将查询更改为您的,谢谢,现在可以添加,但现在的问题是整个列都在更改。 (例如:我添加了 100,现在所有列都添加了 100),当我再次单击添加时,我得到了这个错误(Parameter '@iQuantity' has been defined.
    • cmd 是否在全球范围内声明? 不好。但您可以在命令文本前添加cmd.Parameters.Clear()
    • @AlexLuthor 一,您应该在更新查询中使用 where 子句指定要更新的行(可能带有 id?)。二,你应该使用var cmd = new () 而不是总是使用相同的SqlCommand 字段。换句话说,每次都实例化一个新实例,最好使用using 子句。我希望约翰会修改他的答案以反映这一点。
    • 全局声明?嗯,如果您问我是否在所有内容中都使用了它...我还添加了您给出的行...它有助于消除异常。但是另一个问题仍然存在,如果可以的话,您介意帮我看看吗...我对VB.NET和MySql真的很陌生,我还在学习..
    • @nawfal var cmd = new() 所以在.net 中是dim cmd = new mysqlcommand 对吗?嗯和使用条款?好的,谢谢,我会阅读的...谢谢您的建议..
    猜你喜欢
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 2017-10-14
    • 2013-08-21
    • 1970-01-01
    相关资源
    最近更新 更多