Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect EOF on DataReader in C# without executing Read()

Tags:

c#

ado.net

I'm familiar with using .Read() to detect the EOF

        using (IDataReader reader = SqlHelper.ExecuteReader(_connectionString, "dbo.GetOrders"))
        {
            AssertOrder(reader);

            while (reader.Read())
            {
                yield return FillRecord<Order>(reader, StringComparer.OrdinalIgnoreCase);
            }
            reader.Close();
        }

Due to some weird situation I got myself into, the FillRecord actually advances the reader. So now the .Read() in the while loop actually causes this function to skip some rows -- because we are advancing twice.

I wish there was an IDataReader.EOF, but there isn't. Any thoughts?

like image 257
101010 Avatar asked Oct 21 '11 02:10

101010


2 Answers

I think I probably made my opinion clear in the comments... but, since you asked for "Any Thoughts"... here ya go:

Obviously a two second hack job, but you'll get the idea:

(You'd surely want to implement IDisposable (since I think IDataReader is??) etc. Probably just make the class inherit from IDataReader, and implement the class as a facade. Or get real cool and implement a transparent proxy which hijacks the Read method.

public class DataReaderWithEOF
{
     public bool EOF { public get; private set; }
     private IDataReader reader;

     public DataReaderWithEOF(IDataReader reader)
     {
          this.reader = reader;
     }

     public bool Read()
     {
           bool result = reader.Read();
           this.EOF = !result;
           return result;
     }
}
like image 158
Steve Avatar answered Oct 11 '22 20:10

Steve


using (IDataReader reader = SqlHelper.ExecuteReader(_connectionString, "dbo.GetOrders"))
    {
         if(!reader.HasRows)
         {
              Response.Write("EOF"); // empty
         }
         while (reader.Read()) //read only registers
         {
               yield return FillRecord<Order>(reader, StringComparer.OrdinalIgnoreCase);
          }
          reader.Close();
       }
    }

every time u use a. Read () it next record. then do if(reader.Read()) u would already be advancing a record.

like image 21
Dorathoto Avatar answered Oct 11 '22 18:10

Dorathoto