DataReader to Dataset...?

Posted on 2008-10-31
Last Modified: 2012-06-21
I need to read data from a database on page load, then keep results in memory so to work "offline" and browse through records.

I do need a DataSet, right? If so, how do I fill a DataSet from a DataReader?
Question by:ziggie301

Assisted Solution

rpkhare earned 150 total points
ID: 22853610
I am not following what you are saying. You will a DataSet through Data Adapter. What you are trying to achieve? If you want to simply fill the DataSet, it can easily be done by passing SQL Query to Data Adapter.
LVL 21

Expert Comment

ID: 22853703
Just plug this code in and pass your DataReader object...

DataSet yourset = DataReaderToDataSet(YourDataReader);

and here's the method:

static DataSet DataReaderToDataSet(IDataReader reader)
DataTable table = new DataTable();
int fieldCount = reader.FieldCount;
for (int i = 0; i < fieldCount; i++)
table.Columns.Add(reader.GetName(i), reader.GetFieldType(i));
object[] values = new object[fieldCount];
while (reader.Read())
table.LoadDataRow(values, true);
DataSet ds = new DataSet();
return ds;

with DataAdapter:

SqlDataReader dr = cmd.ExecuteReader();
DataSet ds = new DataSet();
DataSet dt = new DataTable();
DataReaderAdapter da = new DataReaderAdapter();
da.FillFromReader(dt, dr);
LVL 21

Expert Comment

ID: 22853718
By the way, IDataReader is used because it's just the interface that a DataReader must implement and therefore a DataReader is considered a IDataReader (i.e. it's considered a child class that's inheriting methods), so just pass a normal DataReader and it will work...
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

LVL 13

Accepted Solution

TechTiger007 earned 200 total points
ID: 22853909
Why do you need to get a datareader first and convert it into dataset?
Why dont you directly fill data into dataset.

Here is a sample
LVL 21

Assisted Solution

silemone earned 150 total points
ID: 22853964
I agree Tech, to a point so i gave him both ways because I thought similarly to let him see how small the code would be using a data adapter, however, the method for converting datareader to dataset is already written so he would only have one line of code to write that...Code for DataAdapter the code at the bottom of my post, but maybe he has a DataReader already and doesn't feel comfortable changing code around a  lot, so i took that into consideration since that's what he asked for and also gave him that code.  Also, this code is all over the web, so it appears many people convert DataReaders to Datasets.

Author Closing Comment

ID: 31512182
I just could'nt immagine how to fill a dataset. Now I do. :) Thank you

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question