Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

DataReader to Dataset...?

Posted on 2008-10-31
6
1,173 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

792 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