2016-11-23 156 views
-3

我得到错误的简单更新查询动态创建的表,只是不明白这里的错误是..SQL更新查询(语法错误)

C#代码:

cmd2 = new SqlCommand("UPDATE " + tname + "SET [email protected], [email protected], [email protected] WHERE [email protected]", con99); 
      cmd2.Parameters.AddWithValue("@ans", ans); 
      cmd2.Parameters.AddWithValue("@sans", sans); 
      cmd2.Parameters.AddWithValue("@chk", chk); 
      cmd2.Parameters.AddWithValue("@qno", qno); 
      cmd2.ExecuteNonQuery(); //error showing here in vstudio 

错误:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: Incorrect syntax near 'ans'

+2

错字,前'缺少空间“SET ..' –

+1

我还建议将变量_tname_置于方括号之间,以避免将来的语法错误,如果您有一个带有某种保留字或其他非有效系统标识符的表 – Steve

+0

@AlexK。纠正,现在我得到的错误为:附加信息:错误的语法附近')' – ashish

回答

1

至少有一个问题是缺少空格。 Yu需要SET之前的空格。

cmd2 = new SqlCommand("UPDATE " + tname + " SET [email protected], [email protected], [email protected] WHERE [email protected])", con99); 

您不能将表名作为参数。幸运的是,你确实将其他值作为参数。

您应该小心使用AddWithValue()。它可以使打字错误。用正确的类型明确添加更安全。