2010-04-21 17 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 ...」超過

相關問題