Skip to main content

15 main Difference between DataSet and DataReader in asp.net ?


Asp.net developer uses DataSet and DataReader to fetch data from the data source while developing asp.net application. But most of them don’t know exactly what are the main difference between DataSet and DataReader and what to use and when to use out of these two.
Both DataSet and DataReader are widely used in asp.net applications for the same purpose i.e. to get/fetch the data from the database. But one has to know the best practices in developing fast, reliable and scalable application. So I have tried to list some main differences between the DataSet and DataReader which are as follows:
DataSet Vs DataReader
1. DataReader is used to retrieve read-only (cannot update/manipulate data back to datasource) and forward-only (cannot read backward/random) data from a database. It provides the ability to expose the data from database while DataSet is a collection of in-memory tables.
2. DataReader fetches the records from database and stores in the network buffer and gives whenever requests. It releases the records as query executes and do not
wait for the entire query to execute. Hence very fast as compare to the DataSet which releases the data after loading all the data in memory.
3. DataReader is like a forward only recordset. It fetches one row at a time so very less network cost compare to DataSet which fetches all the rows at a time i.e. it fetches all data from the datasource at a time to its memory area.
4. As one row at a time is stored in memory in DataReader it increases application performance and reduces system overheads while there is more system overheads in DataSet as it fetches all the data from the datasource at a time in memory.
5. As DataReader is forward only, we can’t fetch random records as we can’t move back and forth .While in DataSet we can move back and forth and fetch records randomly as per requirement.
6. DataReader fetches data from a single table while DataSet can fetch data from multiple tables.
7. As DataReader can have data from a single table so no relationship can be maintained while relationship between multiple tables can be maintained in DataSet.
8. DataReader is read only so no transaction like insert, update and delete is possible while these transactions are possible in DataSet.
9. DataSet is a bulky object that requires lot of memory space as compared to DataReader .
10. DataReader is a connected architecture: The data is available as long as the connection with database exists while DataSet is a disconnected architecture that automatically opens the connection, fetches the data into memory and closes the connection when done.
11. DataReader requires connection to be open and close manually in code while DataSet automatically handles it.
12. DataSet can be serialized and represented in XML so easily passed around to other tiers but DataReader can't be serialized.
13. DataReader will be the best choice where we need to show the data to the user which requires no manipulation while DataSet is best suited where there is possibility of manipulation on the data.
14. Since DataSet can be serialized it, can be used in wcf services and web service that will return retrieved data. But DataReader can’t be serialized so can’t be used in wcf services and web services.
15. When you need to navigate through the data multiple times then DataSet is better choice e.g. we can fill data in multiple controls But DataReader can only be read once so it can be bound to a single control and requires data to be retrieved for each control.

Comments

Popular posts from this blog

What is cookie? Advantages and disadvantages of cookies?

What is cookie? A cookie is a small piece of text file stored on user's computer in the form of name-value pair. Cookies are used by websites to keep track of visitors e.g. to keep user information like username etc. If any web application using cookies, Server send cookies and client browser will store it. The browser then returns the cookie to the server at the next time the page is requested. The most common example of using a cookie is to store User information, User preferences, Password Remember Option etc.It is also one of the common and mostly asked interview questions. Some facts about Cookie Here are a few facts to know about cookies: · Cookies are domain specific i.e. a domain cannot read or write to a cookie created by another domain. This is done by the browser for security purpose. · Cookies are browser specific. Each browser stores the cookies in a different location. The cookies are browser specific and so a cookie created in one browser(e.g in Google Chrome

Code First Getting Started

In this tutorial let us create a simple application to demonstrate the use of entity framework using code first. We are using Visual Studio 2015 and entity framework 6.1.3. You can download Visual Studio community Edition . You should have the basic knowledge of .Net framework, C# and MS SQL Server. In this tutorial, we will create a simple application with a user class.  Our user class will have basic information like name and email address of the user. Create the Project Open Visual Studio. File ->New -> Project Select C# -> Select Console Application Name the application as “EFGettingStarted” Click on OK Install Entity Framework The next step is to install the Entity framework. This can be installed via nuget package console. Click on Tools->Nuget Package manager -> Package Manager Console and type the following command C# 1 2 3   install - package entityframework   This will install the late

First, FirstOrDefault, Single, SingleOrDefault In C#

For people who are new to LINQ, it is difficult to understand the difference between First, FirstOrDefault, Single, SingleOrDefault. In this blog, I will explain what to use and when.     I will take a simple example to make you understand practically how these methods work.   Consider a class Employee with properties as Id, Name, and Department. class  Employee {    public   int  Id {  get ;  set ; }    public   string  Name {  get ;  set ; }    public   string  Department{  get ;  set ; } } I have a list of Employees: List<Employee> employeeList =  new  List<Employee>(){    new  Employee() { Id = 1, Name =  "Sunny" , Department =  "Technical"  },    new  Employee() { Id=2, Name= "Pinki" , Department = "HR" },    new  Employee() { Id=3, Name= "Tensy" , Department = "Finance" },    new  Employee() { Id=4, Name= "Bobby" , Department = "Technical" },    new