How to skip the first row in sqldatareader

I am extracting 10 mailboxes from my database, but want to skip the first one. The reason is that the first element in my table is already displayed in the main div on my page. Now I want to list all the remaining remaining entries under it. How to do it?

My code is working fine and I can display all the entries in the reader. Now I just need to skip the first one.

+3
source share
4 answers

Just read the first one, then go to the others:

myReader.Read();

while(myReader.Read())
{
   //do stuff
}
+13
source

If you want to use Linq, here is a trick to get it working with DataReaders using a simple extension method:

public static IEnumerable<IDataRecord> AsEnumerable(this IDataReader reader)
{
    while (reader.Read())
    {
        yield return reader;
    }
}

Then you can use the Linq method Skip:

using (var reader = command.ExecuteRead())
{
    foreach(var row in reader.AsEnumerable.Skip(1))
    {
        // whatever you do with the data...
    }
}
+10

reader.Read() , . . - reader.Read() - false, , .

+1

, - "", DataReader, :

  • , SQL-, sproc - ?
  • ? , ?

Your question implies a lack of coding experience - either in general or in relation to a DataReader or something else. Maybe you could get even more help if you explained why you need to skip the first line?

0
source

Source: https://habr.com/ru/post/1753268/


All Articles