Best method for Populating DataSet from a SQLDataReader

Andrew Harry picture Andrew Harry · Feb 4, 2009 · Viewed 30.7k times · Source

I am working on a DAL that is getting a DataReader Asynchronously.

I would like to write a single method for transforming the DataReader into a DataSet. It needs to handle different schema so that this one method will handle all of my fetch needs.

P.S. I am populating the SQLDataReader Asynchronously, please don't give answers that are getting rid of the DataReader.

Answer

Peter Riesz picture Peter Riesz · Jun 22, 2012

DataTable.load() can be used for a generic approach.

do {
    var table = new DataTable();
    table.Load(reader);
    dataset.Tables.Add(table);
} while(!reader.IsClosed);