Jeff's SQL Server Blog

Random Thoughts & Cartesian Products with Microsoft SQL Server
posts - 157, comments - 2686, trackbacks - 64

My Links

Advertisement

News

Welcome to my weblog. My name is Jeff Smith, I am software developer in Boston, MA and I was recently named a 2009 SQL Server MVP. Check in frequently for tips, tricks, commentary and ideas on SQL Server and .NET programming.


Subscribe





Archives

Post Categories

Programming

Sports

SQL

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

Print | posted on Thursday, February 28, 2008 10:34 AM | Filed Under [ Code Library - C# / VB.NET ]

Feedback

Gravatar

# 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.
2/29/2008 7:28 AM | Richard
Gravatar

# 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.
2/29/2008 8:34 AM | Jeff
Gravatar

# 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
3/2/2008 6:44 PM | Jon H.
Gravatar

# re: Simple DataViewReader class that implements IDataReader for a DataView

Very well
3/6/2008 11:04 AM | VENKATA SREEKANTH MARAM
Comments have been closed on this topic.

Powered by:
Powered By Subtext Powered By ASP.NET