Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert datatable to datareader

Tags:

c#

ado.net

For Performance improvement I want to convert datatable to datareader. I can not do that through query. So is there any other way to do so?

like image 243
user1049021 Avatar asked Feb 01 '12 05:02

user1049021


People also ask

How do I create a DataTable from a DataReader?

Create a DataTable schema in the destination DataSet using the schema information returned by the GetSchemaTable ( ) method of the DataReader . Then, use the GetData ( ) method of the DataReader to load each row of data into an array of objects, and add it to the DataTable using the Add ( ) method of the contained DataRowCollection .

How do I transfer data from a DataReader to a dataset?

You need to transfer data from a DataReader to a DataSet . Create a DataTable schema in the destination DataSet using the schema information returned by the GetSchemaTable ( ) method of the DataReader .

Is it possible to convert data reader to data table?

Performance wise a data reader is faster than any of the other way like data adapter and cell set in MDX result. So in few situations you may need to convert this data reader to a data table. Here in this post we will discuss how we can convert it.

What is the use of a DataReader?

A DataReader is typically used to read data from the database and add logic to fill a list of objects or a DataTable. So it's best to do most business logic which has to do with the building of the DataTable on the webservice, pass it to the client as a webservice and work with the other ADO.Net functions there for more business logic.


1 Answers

I know this is old, but the answers here seem to have missed the point of the OPs question.

DataTables have a method called CreateDataReader which will allow you to convert a DataTable to a DbDataReader object. In this case a DataTableReader.

DataTable table = new DataTable(); 
//Fill table with data 
//table = YourGetDataMethod(); 
DataTableReader reader = table.CreateDataReader();

I should point out that this will not increase performance since you should be using one or the other.

Here are some more resources on the matter:

  • DataReader Vs DataTable
  • Is datareader quicker than dataset when populating a datatable?
like image 161
Brandon Boone Avatar answered Sep 27 '22 20:09

Brandon Boone