Convert a DataReader to a DataTable
When working with Datareaders one of the problems i sometimes come across is binding these to 3rd party controls. A lot of controls will not bind to a DataReader but nearly all of the alwaays bind to a DataTable. All of the functionlity already exists in the Framework you just need to know where to look.
So here is the simple way to convert a DataReader to a DataTable.
Create a class Inheriting from DataAdapter
class DataReaderAdapter : System.Data.Common.DataAdapter
{
public int FillFromReader(DataTable dataTable, IDataReader dataReader)
{
return this.Fill(dataTable, dataReader);
}
}
Then to use it :
DataReaderAdapter dar = new DataReaderAdapter();
dar.FillFromReader(dataTable, dataReader);
Attached is a sample project showing the use of this.
| Author |
: Greg Duffield |
| Published |
: Thursday, 13 July, 2006 |
Greg has too many years experience in developement from VB for DOS all the way through to Windows Workflow and LINQ while covering pretty much every technology in between. A complete MS evangelist he is now Director of the only MS Gold Partner IT services company in Norfolk. Wehere they are producing Web 2 Applications for various sectors, and are currently entering the Haulage industry with their latest product.