Creating a DataReader from a DataTable’s original values with two new ADO.NET 2 features

One of the fun brain teasers that was asked in my What’s new in ADO.Net 2.0 talk today at Code Camp 4 in Boston was if it was possible to create a DataReader from the original values of a DataTable using the new CreateDataReader method.

Although there isn’t a direct way to do it, I came up with a simple way to achieve this.

Basically you get a dataview of the table. Set the RowsStateFilter to OriginalRows. (That’s available in 1.x also). Then use the new DataView.ToTable method to create a new table. Lastly, use the Table.CreateDataReader to create a DataTableReader.

dim dv as DataView=myTable.DefaultView
dv.RowStateFilter = DataViewRowState.OriginalRows 
dim dtNew as DataTable=dv.ToTable()
dim dtr as DataTableReader=dtNew.CreateDataReader

Don’t Forget: www.acehaid.org

  Sign up for my newsletter so you don't miss my conference & Pluralsight course announcements!  

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.