VB.NET 和 MySql 更新查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13133068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 10:57:04  来源:igfitidea点击:

VB.NET and MySql UPDATE query

mysqlvb.netwinformsvisual-studio-2010datagridview

提问by Alex Luthor

I have my codes here without errors (at least not when I Debug it, I use VS 2010), but what I wish to happen is when I click on the add button, the number/s in the textbox(txtQty) would add to the numbers currently saved in the column "Quantity". (ex: txtQty "100" and current on the column is "200", I want to add "100" from the textbox to the column which has "200" and save it as a new number "300". Simple math I know, but the problem is I have yet to know how to use math equations on VB.NETDatagridviewor MySql DatabaseThis Code executes but when I click Add the numbers in the column return to 0. Any Hint or Tips would be much appreciated, thanks.

我的代码在这里没有错误(至少在我调试它时没有错误,我使用VS 2010),但我希望发生的是当我点击添加按钮时,文本框(txtQty)中的数字会添加到数字中当前保存在“数量”列中。(例如:txtQty“100”,列上的当前值为“200”,我想将文本框中的“100”添加到具有“200”的列并将其另存为新数字“300”。我知道的简单数学,但问题是我还不知道如何使用数学方程VB.NETDatagridviewMySql 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!")

回答by John Woo

your command text must be parameterized also

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

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

UPDATE 1

更新 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