回答

收藏

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

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

一个简单的问题是如何处理它MS查询中的字段值增加1?我正在尝试int使用参数化方法向我添加1( 1)SQL Server数据库中的列。类似于变量i
- W. l; W8 M  p: s/ g& {        操作。我正在使用以下方法:
1 a5 Y6 Q! }* o, P) A  }0 tpublic 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与加号( )相关的以下错误:
7 k7 g+ r' M8 T# S: i附近的语法不正确。$ A2 F) }& c  D
说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。
5 x2 e: L) K$ Z: u异常细节:System.Data.SqlClient.SqlException:’ ’附近的语法不正确。
% L. \2 |6 m% [0 `- R源错误:
; |4 s$ U9 I4 a3 e2 o0 K第315行:  F9 F  F7 _: F0 m
    第316行:conn.Open();- i: _8 z9 B9 z* r
    第317行:updatesuccess = sqlcmd.ExecuteNonQuery();
# C1 B1 O  m3 U4 J& s    第318行:conn.Close();
/ {) h' G9 Q! e. x9 w5 h    319行:" ]9 F- e; ^+ B0 \9 Y
源文件:c:\ testdevlocation \ appname \ App_Code \ ClassFileName.cs行:317; R5 f9 J( T5 r( K# D8 U
有什么建议吗?3 i. f0 ?. m7 S, r( ?; H$ B0 M2 N+ F
                                                               
4 K9 q1 l+ b8 a3 M6 O6 P    解决方案:                                                                # C1 p! z# l" N7 l: p, v
                                                                您需要一个值和一个字段来分配值。这个值是TableField  1,因此分为:
) w' P  j2 D/ T6 \4 U" s# k" P3 MSET TableField = TableField
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则