回答

收藏

如何在SQL查询中的SQL Server列中添加1( 1)

技术问答 技术问答 471 人阅读 | 0 人回复 | 2023-09-14

一个简单的问题是如何处理它MS查询中的字段值增加1?我正在尝试int使用参数化方法向我添加1( 1)SQL Server数据库中的列。类似于变量i
2 q6 h! p$ @; Y$ W' f7 e2 M        操作。我正在使用以下方法:- ?+ }. U, ^$ I. I5 W
public static int UpdateFieldCount(int parameterId){    / variable to hold the number of rows updated or the success of the query    int updatesuccess = 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与加号( )相关的以下错误:- P$ [1 V+ A, f+ N6 j, B! l
附近的语法不正确。- e7 d; k) O/ N
说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。% Z" q  }! [9 V- O9 w* @, `8 [
异常细节:System.Data.SqlClient.SqlException:’ ’附近的语法不正确。
, y4 h9 V4 G2 I! ~! e. |* j源错误:" }0 O! U" N% @$ N( A, x4 G/ G
第315行:
% d8 f1 e) j0 V) P# F+ b2 a* H    第316行:conn.Open();, ~. q$ @: Z7 W8 \: g2 K
    第317行:updatesuccess = sqlcmd.ExecuteNonQuery();) [7 h% O! n7 Z. S  L( a* B
    第318行:conn.Close();5 g2 o  T: m1 @
    319行:
( M' x9 u7 \2 a, |/ L. a源文件:c:\ testdevlocation \ appname \ App_Code \ ClassFileName.cs行:3172 }/ _! t7 P4 W* z" S* Y
有什么建议吗?
1 L5 Q* N" F* a' E                                                                : ?- x! `& j) x/ c  N7 v, h) ?' [
    解决方案:                                                                % S" s: \' W5 N" ^) `5 ?6 Q
                                                                您需要一个值和一个字段来分配值。这个值是TableField  1,因此分为:
& _0 n! M1 H. p5 d7 ISET TableField = TableField
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则