Jeff Smith Blog

Random Thoughts & Cartesian Products with Microsoft SQL Server

Simple DataViewReader class that implements IDataReader for a DataView

I previously wrote that the .NET 2.0 DataTableReader class is really handy, but unfortunately there is no DataViewReader class.   Thus, the only way to use the IDataReader interface with a sorted/filtered DataView was to first use the ToRows() method of the view to create a brand new DataTable, and then call CreateDataReader() on that new table.   This is not the most efficient process when all you want to do is enumerate a DataView.

So, here's a solution: a simple, efficient, mostly-complete DataViewReader class.   Just create a new instance of it passing in the source DataView and off you go.  This will be much more efficient than creating a brand new DataTable from the DataView, since we just use the default enumerator of the DataView and do not need to copy/create any more things in memory.

Most methods are implemented but a few aren't, feel free to complete the class if you need the missing functionality.  It's pretty simple and straightforward. 

If you find any bugs or have any suggestions for improvement, please let me know.

Click here for the code

Legacy Comments


Richard
2008-02-29
re: Simple DataViewReader class that implements IDataReader for a DataView
The NextResult() method should always return false; it's only used with SQL batches which return multiple result-sets.

Jeff
2008-02-29
re: Simple DataViewReader class that implements IDataReader for a DataView
Thank you so much for noticing that Richard, not sure how I missed that. It's been updated.

Jon H.
2008-03-02
re: Simple DataViewReader class that implements IDataReader for a DataView
Excellent clean code, I will definately be using this and could of used it a while back!

Thanks Jeff

Jon

VENKATA SREEKANTH MARAM
2008-03-06
re: Simple DataViewReader class that implements IDataReader for a DataView
Very well