2014-03-25 27 views
0

我有此查詢一個問題,我想檢索所有記錄,但在離開前二十,添加限制與查詢在asp.net

錯誤是:{「附近有語法錯誤‘限制’。」}

"SELECT * FROM [upload_news] WHERE [country]='" + country.Text + "' ORDER BY [upload_time] DESC LIMIT 20"; 

回答

6

您不能使用SQL Server LIMIT。您可以使用Top 20。或者,您可以使用ROW_NUMBER,然後基於此進行過濾。

你也應該parametrized your query,你當前的查詢很容易SQL Injection

using (SqlCommand cmd = new SqlCommand(@"SELECT TOP 20 * 
             FROM [upload_news] 
             WHERE [country][email protected] ORDER BY [upload_time] DESC", connection)) 
{ 
    cmd.Parameters.AddWithValue("@country", country.Text); 
    //,.... rest of the code 
} 
+1

+1提供一個選項,是不可怕容易注入。 – Zane

2

如果它是SQL Server,則需要使用頂級N.

SELECT TOP 20 * FROM [upload_news] WHERE [country]='" + country.Text + "' 
ORDER BY [upload_time] DESC 
0
,如果你想要檢索的所有記錄

但使用此查詢離開前二十

SELECT * FROM(SELECT(ROW_NUMBER)OVER(ORDER BY upload_time遞減())AS的rowNum,* FROM [ upload_news])作爲temp其中temp.rowNum> 20

,請告訴我,如果它的工作