From: Jehu Galeahsa on
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
From: Jehu Galeahsa on
On May 26, 10:40 pm, Jehu Galeahsa <jehugalea...(a)gmail.com> wrote:
> 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

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 don't 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 don't 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 don't 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.
 | 
Pages: 1
Prev: Reading & writing a colour
Next: File locking