2011-10-13 29 views
3

我在我的頁面上有文本區域。在那個領域我必須添加一些HTML代碼並將其保存到數據庫中。它適用於簡單的HTML,但是當我選擇「維基百科」,例如一些文本,粘貼並嘗試保存在SQL查詢需要執行我得到以下錯誤異常:如何在數據庫中保存HTML內容

Incorrect syntax near 's'. 
The identifier that starts with '. Interestingly, old maps show the name as&nbsp;<em>Krakow</em>.</p> 
<p>Kragujevac experienced a lot of historical turbulence, ' is too long. Maximum length is 128. 
The identifier that starts with '>Paleolithic</a>&nbsp;era. Kragujevac was first mentioned in the medieval period as related to the public square built in a sett' is too long. Maximum length is 128. 
The label 'http' has already been declared. Label names must be unique within a query batch or stored procedure. 
The label 'http' has already been declared. Label names must be unique within a query batch or stored procedure. 
Unclosed quotation mark after the character string '>Belgrade Pashaluk</a>.</p>' 

我使用asp mvc和剃鬚刀引擎。我不知道也許我需要以某種方式編輯html。我也加入了這個爲ArticleText屬性:

[AllowHtml]   
     public string ArticleText { get; set; } 

這是保存到數據庫代碼:

string sql = @"insert into tbl_articles 
           (Text) values 
           ("'" + article.ArticleText"'"+")"; 

       SqlCommand cmd = new SqlCommand(sql, conn); 

       cmd.ExecuteNonQuery(); 
+0

問題是你的ArticleText Contai ns單引號(又名撇號),需要轉義。爲什麼要在這裏構建動態SQL而不是使用參數化查詢? –

+0

製作一個proc,它完全符合你在字符串中的內容,並將HTML作爲參數傳遞......就像魔術一樣。 – SQLMason

回答

28

哇,否,否,否。您的代碼容易受到SQL注入的攻擊,如果您不使用參數化查詢,則會發生非常糟糕的情況。所以使用參數化查詢。

using (var conn = new SqlConnection("some conn string")) 
using (var cmd = conn.CreateCommand()) 
{ 
    conn.Open(); 
    cmd.CommandText = "insert into tbl_articles (Text) values (@Text)"; 
    cmd.Parameters.AddWithValue("@Text", article.ArticleText); 
    cmd.ExecuteNonQuery(); 
} 

每當你使用+運營商構建一個SQL查詢時,你正在做的事情非常危險的,錯誤的連接字符串。

+0

+1 - 是否會開闢道路,但一如既往... –

+0

正是因爲如此,我才把它們放在「使用」聲明中。 –

+0

yeesh我是盲人或愚蠢的或兩者兼而有之。 –

1

這是打開你的系統到Sql injection attack的一個典型的例子。

您需要跳過'字符,因爲如果Html包含'字符,它將在執行SQL語句時破壞它。

編輯:使用Darins解決方案來解決問題。

+3

您需要做的不僅僅是爲了保護您的系統 – zellio

+0

借調。撇號只是冰山的一角。 –

+0

我有點突出了錯誤的原因,然而,解決方案正如你所說的,不僅僅是照顧我的想法 - –

1

嘗試:

string sql = @"insert into tbl_articles 
           (Text) values 
           (@articleText)"; 

       SqlCommand cmd = new SqlCommand(sql, conn); 
       cmd.Parameters.AddWithValue("@articleText", 
       Server.HtmlEncode(article.articleText)); 

       cmd.ExecuteNonQuery(); 
+0

HtmlEncode爲什麼會進入數據庫?我想你會想要這樣做,而不是進入。 –

+0

你需要解碼什麼時候它出去。 – ozsenegal

+1

...所以你想HTMLEncode進入數據庫的文本,然後HtmlDecode從數據庫中出來的文本?爲了誤導一部着名的電影,「我不認爲這意味着你認爲它的意思。」您只需轉義文本,以便正確安全地保存到數據庫中,而不是HtmlEncode;同樣,因爲它已經是HTML,所以當你從數據庫中拉出它時,你可能不想解碼它。 –

2

試圖挽救這種方式:

string sqlQuery = "INSERT INTO tbl_articles (Text) VALUES (@text)"; 
SqlCommand cmd = new SqlCommand(sqlQuery, db.Connection); 
cmd.Parameters.Add("@text", article.ArticleText); 
cmd.ExecuteNonQuery(); 
1

這應該是參數:

public void foo(string connectionString, string textToSave) 
    { 
     var cmdString = "insert into tbl_articles (text) values (@text)"; 
     using (SqlConnection conn = new SqlConnection(connectionString)) 
     { 
      using (SqlCommand comm = new SqlCommand(cmdString, conn)) 
      { 
       comm.Parameters.Add("@text", SqlDbType.VarChar, -1).Value = textToSave; 
       comm.ExecuteNonQuery(); 
      } 
     } 
    } 

(這是gereral想法,這不是一個完全功能的書面)

相關問題