Return top 5 records of a table in a dataset

14,606

Solution 1

If the dataset is already populated, you can use LINQ to take the first 5 rows from a table. (The more efficient method would be to handle this at the database, however.)

Dim rows = DS.Tables("Foo").Rows.Cast(Of DataRow)().Take(5)

Solution 2

How about using the TOP clause. THis assume you are using SQL Server.

ie Select top 5 * from SomeTable

If you are using MySQL there is the limit clause

If you are using Oracle lookup ROWNUM

Solution 3

If you're using SQL Server 2005 or greater, you could use the ROW_NUMBER() function to number the rows, then use:

DS.Tables("TABLENAME").Select("row_number <= 5")
Share:
14,606
JPJedi
Author by

JPJedi

developer

Updated on June 04, 2022

Comments

  • JPJedi
    JPJedi almost 2 years

    I want to return the top 5 records of a table in a dataset for datagrid view. The following does not work.

    DataGridView.DataSource = DS.Tables("TABLENAME").Select("SELECT TOP 5")
    

    Any suggestions?

    Using Visual Studio 2008 - VB.Net