SQL Update Column By Adding A Number To Current Int

前端 未结 4 1546
一生所求
一生所求 2021-01-22 00:39

Simple enough, I can\'t figure out how to add (that\'s +) an integer from a textbox to the integer in the SQL Field.

So for example, the SQL Field may have \'10\' in it

相关标签:
4条回答
  • 2021-01-22 00:58

    You have to use the correct sql:

    Dim sql = "UPDATE PersonsA SET U_G_Studio=U_G_Studio + @ugpoints WHERE Members_ID=@recevierID"
    

    Also use the correct type with AddWithValue:

    Using cmd = New SqlCommand(sql, con)
        ' use the using-statement to dispose everything that implements IDisposable, so also the connection '
            cmd.Parameters.AddWithValue("@ugpoints", Int32.Parse(tranamount.Text)) 
        ' .... '
    End Using
    
    0 讨论(0)
  • 2021-01-22 01:00

    what about

    cmd.Parameters.AddWithValue("@ugpoints",  (int)tranamount.Text) 
    ....
    cmd = New SqlCommand("UPDATE PersonsA SET U_G_Studio= SET U_G_Studio + @ugpoints WHERE Members_ID=@recevierID", con)
    

    edit1: STEVE WAS FASTER!

    0 讨论(0)
  • 2021-01-22 01:03

    The SQL you want is:

    "UPDATE PersonsA SET U_G_Studio= (U_G_Studio + @ugpoints) " & _
    "WHERE Members_ID=@recevierID"
    
    0 讨论(0)
  • 2021-01-22 01:11

    Take the current value of the U_G_Studio field, add the value of the parameter and reassign to U_G_Studio, but keep in mind that you need to pass the value as an integer because otherwise the AddWithValue will pass a string and you get conversion errors coming from the db.

        cmd = New SqlCommand("UPDATE PersonsA SET U_G_Studio=U_G_Studio + @ugpoints " & 
                             "WHERE Members_ID=@recevierID", con)
        cmd.Parameters.AddWithValue("@recevierID", tranmemberID.Text)
        cmd.Parameters.AddWithValue("@ugpoints", Convert.ToInt32(tranamount.Text))
        cmd.ExecuteNonQuery()
    
    0 讨论(0)
提交回复
热议问题