SIGN UP MEMBER LOGIN:    
ARTICLE

DataReader Vs DataSet?

Posted by Suresh Paldia Articles | ADO.NET in C# November 02, 2010
Confused how to use DataSet and How to use DataReader? I am giving out a solution to this.
Reader Level:

Confused how to use DataSet and How to use DataReader?

I am trying to give some of basic understanding about and differences between DataReader and DataSet of ADO.NET.

DataReader
  1. The ADO.NET DataReader is used to retrieve read-only(cannot update data back to datasource) and forward-only(cannot read backward/random) data from a database.
  2. Using the DataReader increases application performance and reduces system overheads. This is due to one row at a time is stored in memory. 
  3. You create a DataReader by calling Command.ExecuteReader after creating an instance of the Command object. 
  4. This is a connected architecture: The data is available as long as the connection with database exists.
  5. You need to open and close the connecton manually in code.
The following code statement is used to retrieve rows from a data source.

//opening connection is must
conn.open();
string SQLquery = "SELECT CustomerID, CompanyName FROM dbo.Customers";
SqlCommand cmd = new SqlCommand(SQLquery, conn);
// Call ExecuteReader to return a DataReader
SqlDataReader myReader = cmd.ExecuteReader();
//The Read method of the DataReader object is used to obtain a row from the results of the executed query. 
while(myReader.Read())
{
    Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
}
//Once you're done with the data reader, call the Close method to close a data reader:
myReader.Close();
//close the connection
conn.close();

DataSet
  1. The DataSet is a in-memory representation of data. 
  2. It can be used with multiple data sources. That is A single DataSet can hold the data from different data sources holdng data from different databases/tables.
  3. The DataSet represents a complete set of data including related tables, constraints, and relationships among the tables. 
  4. The DataSet can also persist and reload its contents as XML and its schema as XML Schema definition language (XSD) schema.
  5. The DataAdapter acts as a bridge between a DataSet and a data source for retrieving and saving data. 
  6. The DataAdapter helps mapping the data in the DataSet to match the data in the data source. 
  7. Also, Upon an update of dataset, it allows changing the data in the data source to match the data in the DataSet. 
  8. No need to manually open and close connection in code.
  9. Hence, point (8) says that it is a disconnected architecture. Fill the data in DataSet and that's it. No connection existence required
The following code statement is used to retrieve rows from a data source.

string SQLquery = "SELECT CustomerID, CompanyName FROM dbo.Customers";
// create DataSet that will hold your Tables/data
DataSet ds = new DataSet("CustomerDataSet");
//Create SqlDataAdapter which acts as bridge to put the data in DataSet,(data is table available by executing your SQL query)
SqlDataAdapter myAdapter = new SqlDataAdapter(SQLquery, conn);
//fill the dataset with the data by some name say "CustomersTable"
myAdapter.Fill(ds,"CustomersTable");

Login to add your contents and source code to this article
share this article :
post comment
 

thank you for your explain different between them

Posted by hilda r Feb 13, 2011

Thank You Sivaraman..
You can check the reply i posted on Akhil's doubt above your comment..

Posted by Suresh Paldia Nov 03, 2010

Thank You Akhil... and good question
When the query is executed, the first row is returned to the DataReader via the stream and is stored on the client. The DataReader's Read() method goes to database and reads the next row, stores it on client.
The stream then remains connected to the database, poised to retrieve the next record. So, For the Read() method to retrieve the next row, your connection to database must exist. So, using DataReader you can only handle one row at a time.
It does not store all records at client on query execution as in case of DataSet.

Now When to use a DataReader?

When populating a list or retrieving 10,000 records. When a huge amount of data must be retrieved to a business process, it can take a while to load a DataSet, pass the data to it from the database, and then store it in memory. However, in a Web application where hundreds of users may be connected, scalability would become a problem. If this data is intended to be retrieved and then traversed for business rule processing, the DataReader could speed up the process as it retrieves one row at a time and does not require the memory resources that the DataSet requires.

Let me know if you stil have any doubt.. I'l go in more detail if pos..

Posted by Suresh Paldia Nov 03, 2010

Well written, useful informations.
I echo the Queation raised by Akhil.

Posted by Sivaraman Dhamodaran Nov 03, 2010

First of All Thanks for giving valuable information about the Data Reader and Data Set.


I would like know about Data Reader in Depth means as you said it is a connected architecture? But my question is that when we make a loop on the data reader it goes each time to the database to get the next record ? Or when we open a Data Reader it gets all the record from the database and keep in memory, and gives one by one record from memory one by One ?

Please Clarify...
Thanks



Posted by Akhil Kumar Nov 03, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor