2010-04-21 20 views
1

后面没有加上引号我只是想知道是否有人可以在这里指向正确的方向,我想我已经看了太久,所以看不到错误。''附近的语法不正确。在字符串“'

下面的代码:

SqlCommand updateStyle = new SqlCommand("UPDATE [Lorenz].[dbo].[Layout] SET [bgColour] = '" + bgColour + "' , [textColour] = '" + txtColour + "WHERE <[LoweredUserName] ='" + currentUser + "' ", connection); 
updateStyle.ExecuteNonQuery(); 

是给错误:

附近有语法错误 '管理员'。 字符串“'后面未加上引号。

回答

7
[textColour] = '" + txtColour + "WH 

缺少一个单引号:

[textColour] = '" + txtColour + "'WH 

编辑:虽然我只是指出了为什么错误发生的事情,我下面的海报是关于使用了这些事情参数化查询正确的;或者可能是一个ORM,如LINQ

0

这是一个来自SQL的语法错误,显然不是来自C#。

您需要获取您在运行时插入的值 - 然后您会看到SQL语句中的语法错误。

编辑或者你可以只是做@zincorp说什么:)

按照一般的做法,这是更具可读性在这种类型的情况下使用String.Format。更重要的是,你也想确保你逃脱文字。

6

我想你应该宁可看看

SQL Parameters in C#

SqlParameter Class

要尽量避免SQL injection

SQL injection is a code injection technique that exploits a security vulnerability occurring in the database layer of an application. The vulnerability is present when user input is either incorrectly filtered for string literal escape characters embedded in SQL statements or user input is not strongly typed and thereby unexpectedly executed. It is an instance of a more general class of vulnerabilities that can occur whenever one programming or scripting language is embedded inside another. SQL injection attacks are also known as SQL insertion attacks

总而言之,你在留下一个巨大的缺口您的应用程序基于动态创建的查询字符串进行攻击。所引用的错误将被处理,但也可以避免,可以说DROP TABLE用户

+1

嗨观众,谢谢你的关注。我知道SQL注入,而我目前只是试图让一些工作,然后我会重新考虑它更安全。 也没有这些变量被分配来自查询字符串的值,例如,当前用户来自 'Page.User.Identity.Name.ToString();' – Mattec 2010-04-21 19:28:25

+0

在凹槽中滑动一次很容易。早些时候进入哈比特,而不是晚些时候。它会为你节省很多时间。这个查询很容易使用正确的方法。无论如何,祝你好运X-) – 2010-04-21 19:33:04

11

你应该真的在使用SQL参数。它不仅有助于保护您的应用程序免受SQL注入攻击,还会使SQL语法错误更容易被发现。

SqlCommand updateStyle = new SqlCommand("UPDATE [Lorenz].[dbo].[Layout] SET [bgColour] = @bgColour, [textColour] = @textColour WHERE <[LoweredUserName] = @currentUser", connection); 
updateStyle.Parameters.Add(new SqlParameter("@bgColour", bgColour)); 
updateStyle.Parameters.Add(new SqlParameter("@textColour", textColour)); 
updateStyle.Parameters.Add(new SqlParameter("@currentUser", currentUser)); 
updateStyle.ExecuteNonQuery(); 
+0

谢谢,我现在将执行此操作:) – Mattec 2010-04-21 19:29:28

1

WHERE <[LoweredUserName]

上述语法尤其是<似乎不正确。尝试在SQL服务器上运行SQL事件探查器(如果适用)以查看发送到服务器的SQL。

还使用参数来防止SQL注入攻击。

0

事实上参数SQL是安全的 - 我使用它,以及:

string mySqlStmt = "UPDATE tbSystem SET systemCode_str = @systemCode_str, systemName_str = @systemName_str"; 

      using (var conn = new SqlConnection(myConnStr)) 
      using (var command = new SqlCommand(mySqlStmt, conn) 
      { 

       CommandType = CommandType.Text 

      }) 
      { 
       //add your parameters here - to avoid SQL injection 
       command.Parameters.Add(new SqlParameter("@systemCode_str", "ABZ")); 
       command.Parameters.Add(new SqlParameter("@systemName_str", "Chagbert's Shopping Complex")); 

       //now execute SQL 
       conn.Open(); 
       command.ExecuteNonQuery(); 
       conn.Close(); 
      } 

你会注意到,与参数化的SQL我没有在我的价值观担心引号中的引号“'” “Chagbert的Shoppin ...”超过

相关问题