Solved

DataReader to Dataset...?

Posted on 2008-10-31
6
1,151 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 13

Accepted Solution

by:
TechTiger007 earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
I just could'nt immagine how to fill a dataset. Now I do. :) Thank you
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

14 Experts available now in Live!

Get 1:1 Help Now