If you want the last 25 rows, then you have to sort them in descending order and select the top 25 rows.
If you want those rows to be sorted in ascending order, you can either sort them in your code after you've retrieved them, or use a nested query:
SELECT * FROM (SELECT TOP 25 * FROM [YourTable] ORDER BY [IndexID] DESC) ORDER BY [IndexID] ASC
NB1: Avoid using
SELECT * FROM ...
; instead, explicitly list the columns you want to return. This will ensure that the columns are returned in the expected order, and you don't end up returning too much data if you add more columns to the table.
NB2: If your
TableNameTcp
variable can be influenced in any way by the user, then you have introduced a
SQL Injection[
^] vulnerability to your code. If you can't use a hard-coded table name, then you need to verify that the supplied table name is one of the existing table or view names.