Solved

DataReader to Dataset...?

Posted on 2008-10-31
6
1,159 Views
Last Modified: 2012-06-21
Hi.
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?
0
Comment
Question by:ziggie301
6 Comments
 
LVL 8

Assisted Solution

by:rpkhare
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.
0
 
LVL 21

Expert Comment

by:silemone
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));
}
table.BeginLoadData();
object[] values = new object[fieldCount];
while (reader.Read())
{
reader.GetValues(values);
table.LoadDataRow(values, true);
}
table.EndLoadData();
DataSet ds = new DataSet();
ds.Tables.Add(table);
return ds;
}

with DataAdapter:



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

Expert Comment

by:silemone
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...
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 13

Accepted Solution

by:
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
http://www.windowsitlibrary.com/Content/1205/06/2.html
0
 
LVL 21

Assisted Solution

by:silemone
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.
0
 

Author Closing Comment

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

912 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now