問題描述
很簡單,我不知道如何將文本框中的整數添加(即 +)到 SQL 字段中的整數.
Simple enough, I can't figure out how to add (that's +) an integer from a textbox to the integer in the SQL Field.
例如,SQL 字段中可能包含10",文本框中可能包含5".我想將這些數字加在一起以存儲15",而無需下載 SQL 表.
So for example, the SQL Field may have '10' in it and the textbox may have '5' in it. I want to add these numbers together to store '15' without having to download the SQL Table.
包含要添加到 SQL 整數的整數的文本框是 tranamount.Text
,SQL 表中的 SQL 列是 @ugpoints.請注意,如果沒有+"——這在下面的代碼中是公認的錯誤——tranamount.Text
的值被添加到表中沒有問題,但它只是替換了原始值;這意味著最終結果在 SQL 字段中將是5".
The textbox that contains the integer to be added to the SQL integer is tranamount.Text
and the SQL Column in the SQL Table is @ugpoints. Please note, without the '+' - which is in the below code and is admittedly wrong- the value of tranamount.Text
is added to the Table without an issue, but it simply replaces the original value; meaning the end result would be '5' in the SQL Field.
構建它的正確方法是什么?我已經嘗試了下面的代碼,但這顯然不起作用.
What would be the proper way to structure this? I've tried the below code, but that clearly doesn't work.
cmd = New SqlCommand("UPDATE PersonsA SET U_G_Studio=@ugpoints WHERE Members_ID=@recevierID", con)
cmd.Parameters.AddWithValue("@recevierID", tranmemberID.Text)
cmd.Parameters.AddWithValue("@ugpoints", + tranamount.Text) '<--- Value to add.
cmd.ExecuteNonQuery()
新手問題我知道,我是 vb 中的 SQL 新手.
Newbies question I know, I'm new to SQL in vb.
推薦答案
取U_G_Studio
字段的當前值,加上參數的值,重新賦值給U_G_Studio
,但請記住,您需要將值作為整數傳遞,否則 AddWithValue
將傳遞一個字符串,并且您會收到來自數據庫的轉換錯誤.
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()
這篇關于通過向當前整數添加數字來更新 SQL 列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!