---
title: "What is datareader and dataset in C#?"  
description: "What is datareader and dataset in C#?"  
author: "Anonymous User"  
published: 2020-01-22  
updated: 2020-01-22  
canonical: https://www.mindstick.com/forum/155720/what-is-datareader-and-dataset-in-c-sharp  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# What is datareader and dataset in C#?

[explain me](https://www.mindstick.com/forum/34220/can-anyone-explain-me-about-off-page-activity) about datareader and dataset in C# with image.

![What is datareader and dataset in C#?](https://www.mindstick.com/mindstickforums/13063281-0f8c-4f08-a8b5-b5d98e4f16f0/images/61ee6863-e28d-4240-9383-c964a4b97670.jpeg)

## Replies

### Reply by Nishi Tiwari

In C#, we can work in two different ways with the database connectivity. As we know when we create software or any website or any other application which is connected to the database we must need to make connection to the database to get data. Here we can get data in two different ways.

Either it may be connected architecture where we go and connect to the database and get data or disconnected architecture where we connect to the database first time and get all data in an object and use if it is required. We can perform any action like insert, update, and search on this disconnected architecture.

We can use two C# objects to achieve this, first one is **[DataSet and other one is DataReader.](https://www.mindstick.com/forum/155711/what-is-if-else-statement-in-c-sharp)**

**DataSet** \

In this type of disconnected architecture we don’t need to connect always when we want to get data from the database. We can get data from dataset and DataSet is a collection of datatables. We can store the database table, view data in the DataSet and can also store the xml value in dataset and get it whenever required, the data retrieved from database can be accessed even when connection to the database was closed. It is built on classesconnection, dataadapter, commandbuilder, dataset and dataview.

![What is datareader and dataset in C#?](https://www.mindstick.com/mindstickforums/13063281-0f8c-4f08-a8b5-b5d98e4f16f0/images/c27fb1fb-9b98-45fd-841c-ac4ca91e083e.png)\

To achieve this we need to use DataAdapter which work as a mediator between Database and DataSet.

## Example

```
1.	public DataSet GetEmployeeData()
2.	{
3.	    SqlConnection conString = new SqlConnection("myconnection");
4.	    conString.Open();
5.	    SqlCommand cmdQuery = new SqlCommand("Select * from Employee", conString);
6.	    SqlDataAdapter sda = new SqlDataAdapter(cmdQuery);
7.	    DataSet dsData = new DataSet();
8.	    sda.Fill(dsData);
9.	    return dsData;
10.	}
```

## \

## DataReader

It is a type of connected architecture, which means when we require data from the database we need to connect with database and fetch the data from the database. We can use if we need updated data from the database in a faster manner. DataReader is Read/Forward only that means we can only get the data but we cannot update or insert the data into the database. It fetche the record one by one within the database.

In connected architecture, we know the connection must be opened to access the data retrieved from the database. It is also built on the classes connection, command, datareader and transaction.

## Example

```
1.	static void HasRows(SqlConnection connection)
2.	{
3.	    using (connection)
4.	    {
5.	        SqlCommand command = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories;",connection);
6.	        connection.Open();
7.	        SqlDataReader reader = command.ExecuteReader();
8.	        if (reader.HasRows)
9.	        {
10.	            while (reader.Read())
11.	            {
12.	                Console.WriteLine("{0}\t{1}", reader.GetInt32(0),reader.GetString(1));
13.	            }
14.	        }
15.	        else
16.	        {
17.	            Console.WriteLine("No rows found.");
18.	        }
19.	        reader.Close();
20.	    }
21.	}
```

![What is datareader and dataset in C#?](https://www.mindstick.com/mindstickforums/13063281-0f8c-4f08-a8b5-b5d98e4f16f0/images/1abb0db9-6e39-4960-884a-62389e117c85.png)\


---

Original Source: https://www.mindstick.com/forum/155720/what-is-datareader-and-dataset-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
