Bhaskar
Bhaskar

Reputation: 4259

Fetch specific rows from DB through ADO.Net

I have a table containing 10 records. I want to fetch 5th and the 7th row from it. Is there any way of achieving this in ADO.NET.

Upvotes: 0

Views: 329

Answers (3)

gbianchi
gbianchi

Reputation: 2138

Usually you don't recover a specific row when you return a query. Your 5th row could be some specific data this time, but could change anytime, since no db engine ensure you that a query will return that specific row, unless you can definitive specify it with a where clause.
In a weird case that you want to return a specific row, that would change depending on the database you are using (you can use a combination of rownumber in oracle, or top in sql server).

Upvotes: 1

Eoin Campbell
Eoin Campbell

Reputation: 44278

If your table, is really that small (i.e. 10 rows) why not just retrieve all 10 rows, and iterate through them to get to the 5th/7th rows... as far as directly selecting them goes... How is your table key'd. I.e. What dictates what the 5th and 7th rows are?

Are you applying an ORDER BY on your query...

If you wanted to get the 7th row from the top based on some specific order, you could do something UGLY Like.

SELECT 
    Top 1 * 
FROM 
(
    SELECT 
        Top 7 * 
    FROM 
        TABLE ORDER BY 1 ASC
) as a
ORDER BY 1 DESC

OR if you're working with SQL2005/Oracle/Another DBMS that has a RowNumber() function like is used for paging then that would work too, but it completely depends on the structure of your table, the database you're using & how you're retrieving that information...

Upvotes: 1

TheTXI
TheTXI

Reputation: 37895

That depends a lot on what type of database you are using. Different databases such as SQL Server and MySQL have different methods of handling "paging" which is basically what you are wanting to accomplish. Even different versions such as SQL Server 2000 has different methods than 2005 and so on.

Here is one source that could help you get started to understanding the concept and implementation. Here is another which may be closer to what you are looking for.

Upvotes: 0

Related Questions