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?
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;
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With