如何在SQL查询中的SQL Server列中添加1( 1)
技术问答
470 人阅读
|
0 人回复
|
2023-09-14
|
一个简单的问题是如何处理它MS查询中的字段值增加1?我正在尝试int使用参数化方法向我添加1( 1)SQL Server数据库中的列。类似于变量i& @0 `4 M9 Y" V1 N# m
操作。我正在使用以下方法:
0 S% d' C Q# B$ X7 O6 \3 Cpublic 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与加号( )相关的以下错误: V; z- u8 y1 S8 o& @! A/ \
附近的语法不正确。3 \" l8 @: A# m/ T( x& |
说明:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息。
5 @4 K4 U& U9 q6 p+ S异常细节:System.Data.SqlClient.SqlException:’ ’附近的语法不正确。. W; i6 r7 V7 p% r& K* k
源错误:8 H3 D! ~0 Y% T) x- {5 A8 K9 L
第315行:2 o* F% o7 U+ n0 P) O9 {
第316行:conn.Open();
4 T" ~5 W! J1 D5 ^% n 第317行:updatesuccess = sqlcmd.ExecuteNonQuery();1 @" ]/ h+ N- k
第318行:conn.Close();
- E8 q' ?7 s1 k6 Z) o' }* O4 e 319行:: N2 @) K) |2 v. {' ^/ z. J
源文件:c:\ testdevlocation \ appname \ App_Code \ ClassFileName.cs行:317; s% s8 v% c m5 v1 N! W* e# ^) f
有什么建议吗?# b8 V3 y1 [7 M7 o7 ~; @
) y. J& K5 B8 T4 k
解决方案: ' i( A" ?) f: B" K9 I, ?
您需要一个值和一个字段来分配值。这个值是TableField 1,因此分为:/ l0 P9 @# N. F, _
SET TableField = TableField |
|
|
|
|
|