問(wèn)題描述
很簡(jiǎn)單,我不知道如何將文本框中的整數(shù)添加(即 +)到 SQL 字段中的整數(shù).
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".我想將這些數(shù)字加在一起以存儲(chǔ)15",而無(wú)需下載 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 整數(shù)的整數(shù)的文本框是 tranamount.Text
,SQL 表中的 SQL 列是 @ugpoints.請(qǐng)注意,如果沒有+"——這在下面的代碼中是公認(rèn)的錯(cuò)誤——tranamount.Text
的值被添加到表中沒有問(wèn)題,但它只是替換了原始值;這意味著最終結(jié)果在 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.
構(gòu)建它的正確方法是什么?我已經(jīng)嘗試了下面的代碼,但這顯然不起作用.
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()
新手問(wèn)題我知道,我是 vb 中的 SQL 新手.
Newbies question I know, I'm new to SQL in vb.
推薦答案
取U_G_Studio
字段的當(dāng)前值,加上參數(shù)的值,重新賦值給U_G_Studio
,但請(qǐng)記住,您需要將值作為整數(shù)傳遞,否則 AddWithValue
將傳遞一個(gè)字符串,并且您會(huì)收到來(lái)自數(shù)據(jù)庫(kù)的轉(zhuǎn)換錯(cuò)誤.
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()
這篇關(guān)于通過(guò)向當(dāng)前整數(shù)添加數(shù)字來(lái)更新 SQL 列的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!