DataSet and DataReader are two common components of ADO.NET that are used to get and store data in a C# application. Let's learn the difference between the two and when to use a DataSet vs a DataReader.
DataReader
- The ADO.NET DataReader is used to retrieve read-only (cannot update data back to a datasource) and forward-only (cannot read backward/random) data from a database.
- Using of a DataReader increases application performance and reduces system overheads. This is due to one row at a time is stored in memory.
- You create a DataReader by calling Command.ExecuteReader after creating an instance of the Command object.
- This is a connected architecture: The data is available as long as the connection with database exists.
- 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
- The DataSet is a in-memory representation of data.
- 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.
- The DataSet represents a complete set of data including related tables, constraints, and relationships among the tables.
- The DataSet can also persist and reload its contents as XML and its schema as XML Schema definition language (XSD) schema.
- The DataAdapter acts as a bridge between a DataSet and a data source for retrieving and saving data.
- The DataAdapter helps mapping the data in the DataSet to match the data in the data source.
- Also, Upon an update of dataset, it allows changing the data in the data source to match the data in the DataSet.
- No need to manually open and close connection in code.
- 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");
Here is a detailed tutorial on DataSet: DataSet in C#
Learn more:

Omar MPosted Nov 7, 2017, 2:27 AM
Thanks for your wonderful article i have question if some one wants to fill retrieved data to related business objects wouldn't this remove feature mentioned in DataReader point 2,and will also be slower than dealing with dataset since we need to fill record and map them to business object?
lalit raghuvbanshiPosted May 16, 2013, 1:58 AM
15 main Difference between DataSet and DataReader in asp.net http://www.webcodeexpert.com/2013/04/15-main-difference-between-dataset-and.html
Davood RiaziPosted Aug 19, 2012, 3:24 PM
Thank you, It was very useful article for me actualy,
hilda rPosted Feb 13, 2011, 6:34 AM
thank you for your explain different between them
Sivaraman DhamodaranPosted Nov 3, 2010, 2:25 AM
Well written, useful informations. I echo the Queation raised by Akhil.
Akhil KumarPosted Nov 3, 2010, 12:50 AM
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