Ok, I would like to extract a DataRow
out a DataReader
. I have been looking around for quite some time and it doesn't look like there is a simple way to do this.
I understand a DataReader
is more a collection of rows but it only read one row at the time.
So my question: is there any way to extract a DataRow
out the current row of a DataReader
?
You're probably asking because you have
DataReader
code that looks something like this:But as Tim pointed out, if you know in advance that you're going to want the
DataRow
at each iteration, you should useDataTable
instead, and then just iterate on itsRows
property (following output is identical to above):If you absolutely must continue using the
DataReader
for some reason, I suppose you could add a loop index integer to that first example, and then grab each row from the (fully-pre-loadedDataTable
) on each iteration. But since theDataTable
is richer all-around, there's really no reason not to abandon theDataReader
after doing theDataTable.Load()
.No, at least no simple way. Every DataRow belongs to one Table. You cannot leave that property empty, you cannot even change the table(without using ImportRow).
But if you need DataRows, why don't you fill a
DataTable
in the first place?If you really need to get the row(s) from the
DataReader
you can usereader.GetSchemaTable
to get all informations about the columns:But that is not really efficient.