1

I have a problem with an SQL query.

I use Visual Studio 2015 to test my website, and WebMatrix.Data.Database to do my queries.

Anyways, I am creating a reply system and I use this query to get the replies :

SELECT * 
FROM ThreadReply 
WHERE ThreadId = " + ThreadId + " 
ORDER BY ReplyId DESC

I know there is no prevention against SQL injections so please don't ask me to fix that.

Want I want to add the the query is to start from a certain row and continue for a certain amount of rows, for example; I mean like the LIMIT command where you can select the rows you want to start at but apparently it doesn't work on Visual Studio.

Also, please note, I want the row from the query with the rows that have the WHERE keyword true, so not the row of the actual table.

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Vitulus
  • 634
  • 3
  • 10

1 Answers1

2

Here you go, the comments of the sql are pretty clear I think. Also I fix your sql injection, you just need to add the SqlCommand.Parameters

SELECT 
    * 
FROM 
    ThreadReply 
WHERE 
    ThreadId=@ThreadID 
ORDER BY 
    ReplyId DESC
OFFSET 
    10 ROWS -- skip 10 rows
FETCH NEXT 
    10 ROWS ONLY -- take 10 rows
mybirthname
  • 16,991
  • 3
  • 29
  • 48
  • This syntax is supported from 2012 onwards. For older versions, see this question http://stackoverflow.com/questions/2135418/equivalent-of-limit-and-offset-for-sql-server – Jonas Høgh Mar 13 '16 at 05:53