.NET Framework - DbDataReader.HasRows - Whose Idea was that?

Asked By Jehu Galeahsa on 27-May-10 12:40 AM
Hello:

I am currently writing a small library that will allow reading
formatted plain text files as though they were database results. I am
inheriting from DbDataReader in order to allow for the conversion
between strings and the desired type.

Originally, I was only implementing IDataReader, but am now working on
supporting the System.Data.Common classes. Unfortunately, DbDataReader
introduces two new members, GetEnumerator and HasRows. I am not really
sure how to write either of these. For instance, what is the
GetEnumerator method returning? IDataRecords? More importantly, how
can I tell whether there are records without first reading, for
HasRows?

I'd like some insight on how to implement these. Thanks!

~Travis




Jehu Galeahsa replied to Jehu Galeahsa on 28-May-10 08:57 PM
In case anyone comes across this post, I did find a solution.
Essentially, the interface hints that a database vendor will know
whether a SQL generated results and that some or all of those results
will exist in memory. Personally, I think this is a mistake and
assumes too much about a vendor's capabilities. Especially in my
situation where I really do not know whether I have records until after
I start reading the file.

It is not practical for me to read everything into memory. So, I
decided that I would create a buffer. I will only parse 5 or so
records at a time. When I parse, I do not actually move the buffer
pointer forward. That way I can parse a few records and see if they
exist, without actually "reading" them out of existance.

This was pretty tricky because if I encounter bad data, I do not want
to throw immediately. It is more intuitive if an error occurs while
the user is looking at the bad data. Eventually, I ended up using a
buffer, but it could only hold one record at a time. It seems strange,
but it definitely works.