如何在SQL查询中向SQL Server列添加加一(+1)

the*_*gul 39 sql t-sql sql-server

简单的问题是,如何将MS Query中的字段值增加1?我想1(+1)添加到int在使用参数化方法,我的SQL Server数据库列.类似于对变量的i ++操作.我使用以下方法:

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;
}
Run Code Online (Sandbox Code Playgroud)

此方法在我的sql查询中抛出与加号(+)相关的​​以下错误:

'+'附近的语法不正确.

描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.

异常详细信息:System.Data.SqlClient.SqlException:'+'附近的语法不正确.

来源错误:

第315行:
第316行:conn.Open();
第317行:updatesuccess = sqlcmd.ExecuteNonQuery();
第318行:conn.Close();
319行:

源文件:c:\ testdevlocation\appname\App_Code\ClassFileName.cs行:317

有什么建议吗?

Guf*_*ffa 68

您需要值和字段来分配它.值是TableField + 1,所以赋值是:

SET TableField = TableField + 1
Run Code Online (Sandbox Code Playgroud)


Ash*_*ynd 56

"UPDATE TableName SET TableField = TableField + 1 WHERE SomeFilterField = @ParameterID"
Run Code Online (Sandbox Code Playgroud)