I have a query that I run against the database, and I can see that there is a record for 31/05/2013. When I run this query from C# with ADO.NET, and then use the following
There are two ways of iterating through a data-reader; one is to keep calling .Read()
; the other is to treat it as an IEnumerable
sequence of IDataRecord
and foreach
it; no matter which you choose you can only iterate the data once.
.Read()
moves from the BOF to the first record, if one
ToList()
calls GetEnumerator()
then MoveNext()
in a loop, which immediately moves forward one position (so we've dropped the first record on the floor without processing it); at the end of the ToList()
we have chewed through all the data.Read()
will then report false
(EOF)Basically: the problem here is using two APIs that progress the position. Either use .Read()
, or use the foreach
API (.ToList()
).
As a side note, since the column names match the member names you could also use "dapper" to do the heavy lifting:
var list = conn.Query(sql, args).ToList();