問題描述
簡單的問題是,如何將 MS Query 中的字段值加 1?我正在嘗試使用參數化方法將 1 (+1) 添加到我的 SQL Server 數據庫中的 int
列.類似于對變量的 i++ 操作.我正在使用以下方法:
The simple question is, how do you increment a field value in a MS Query by 1 ? I am trying to add 1 (+1) to an int
column in my SQL Server database using a parametrized method. Similar to an i++ operation on a variable. I am using the following method:
public static int UpdateFieldCount(int parameterId)
{
// variable to hold the number of rows updated or the success of the query
int updatesuccess = 0;
// build your connection string
string connectionstring = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionstring);
// build your SQL Query statement
string SQLString = "UPDATE TableName SET TableField + 1 WHERE SomeFilterField = @ParameterID";
SqlCommand sqlcmd = new SqlCommand(SQLString, conn);
sqlcmd.Parameters.AddWithValue("@ParameterID", parameterID);
conn.Open();
updatesuccess = sqlcmd.ExecuteNonQuery();
conn.Close();
return updatesuccess;
}
此方法在我的 sql 查詢中拋出與加號 (+) 相關的以下錯誤:
This method is throwing the following error related to the plus sign (+) in my sql query:
'+' 附近的語法不正確.
Incorrect syntax near '+'.
描述:在執行當前 Web 請求期間發生了未處理的異常.請查看堆棧跟蹤以獲取有關錯誤及其在代碼中的來源的更多信息.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
異常詳細信息:System.Data.SqlClient.SqlException:+"附近的語法不正確.
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '+'.
源錯誤:
第 315 行:
第 316 行:conn.Open();
第 317 行:updatesuccess = sqlcmd.ExecuteNonQuery();
第 318 行:conn.Close();
第 319 行:
Line 315:
Line 316: conn.Open();
Line 317: updatesuccess = sqlcmd.ExecuteNonQuery();
Line 318: conn.Close();
Line 319:
源文件:c:\testdevlocation\appname\App_Code\ClassFileName.cs 行:317
Source File: c:\testdevlocation\appname\App_Code\ClassFileName.cs Line: 317
對此有什么建議嗎?
推薦答案
您需要一個值和一個字段來分配它.值為TableField + 1
,所以賦值為:
You need both a value and a field to assign it to. The value is TableField + 1
, so the assignment is:
SET TableField = TableField + 1
這篇關于如何向 SQL 查詢中的 SQL Server 列添加加一 (+1)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!