---
title: "Explain the ExecuteNonQuery, ExecuteScalar and ExecuteReader methods of the Command object."  
description: "Explain the ExecuteNonQuery, ExecuteScalar and ExecuteReader methods of the Command object."  
author: "Anubhav Sharma"  
published: 2026-01-11  
updated: 2026-01-13  
canonical: https://answers.mindstick.com/qa/116279/explain-the-executenonquery-executescalar-and-executereader-methods-of-the-command-object  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 3 minutes  

---

# Explain the ExecuteNonQuery, ExecuteScalar and ExecuteReader methods of the Command object.

## Answers

### Answer by Ravi Vishwakarma

In **ADO.NET**, the `Command` object (such as `SqlCommand`) provides several [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) methods depending on **what type of result** you expect from the database.

The three most commonly used methods are:

1. [**ExecuteNonQuery()**](https://www.mindstick.com/blog/430/executenonquery-executereader-and-executescalar-methods-in-c-sharp)
2. **ExecuteScalar()**
3. [**ExecuteReader()**](https://www.mindstick.com/interview/23359/explain-the-executereader-method-in-ado-dot-net)

## 1. ExecuteNonQuery()

### Purpose

Used when you want to **execute a SQL statement that does NOT return result rows**.

### Common Use Cases

- `INSERT`
- `UPDATE`
- `DELETE`
- `CREATE`, `ALTER`, `DROP`
- [Stored procedures](https://www.mindstick.com/forum/540/using-stored-procedures-with-entity-framework-in-an-asp-dot-net-application) that do not return result sets

### Return Value

- Returns an **int** → number of rows affected
- Returns **-1** for some statements like table creation

### Example

```cs
using (SqlCommand cmd = new SqlCommand(
    "UPDATE Users SET IsActive = 1 WHERE Role = 'Admin'", conn))
{
    int rowsAffected = cmd.ExecuteNonQuery();
}
```

### When to Use

- When you only care whether the operation succeeded
- When no data needs to be read back

## 2. ExecuteScalar()

### Purpose

Used when you want to **retrieve a single value (one row, one column)** from the database.

### Common Use Cases

- `SELECT COUNT(*)`
- `SELECT MAX(Id)`
- `SELECT @@IDENTITY`
- Getting a single configuration value

### Return Value

- Returns an **object**
- Returns the value of the **first column of the first row**
- Returns `null` if result set is empty

### Example

```cs
using (SqlCommand cmd = new SqlCommand(
    "SELECT COUNT(*) FROM Users", conn))
{
    int userCount = (int)cmd.ExecuteScalar();
}
```

### Important Notes

- Extra rows or columns are **ignored**
- You must cast the result to the expected type

## 3. ExecuteReader()

### Purpose

Used when you want to **read [multiple](https://www.mindstick.com/forum/2323/handling-multiple-submit-button-in-asp-dot-net-mvc) [rows and columns](https://www.mindstick.com/forum/161180/how-to-compare-rows-and-columns-in-sql-server-database)** from the database.

### Common Use Cases

- `SELECT` queries [returning](https://www.mindstick.com/news/2477/returning-ceo-bob-iger-dismisses-the-disney-apple-sale-as-pure-speculation) multiple records
- Reading large [datasets](https://www.mindstick.com/forum/852/insert-data-from-forms-into-datasets) efficiently

### Return Value

- Returns a `SqlDataReader`
- Provides **forward-only, read-only** access to data

### Example

```cs
using (SqlCommand cmd = new SqlCommand(
    "SELECT Id, Name FROM Users", conn))
using (SqlDataReader reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        int id = reader.GetInt32(0);
        string name = reader.GetString(1);
    }
}
```

### When to Use

- When performance matters
- When working with large result sets

## Comparison Table

| Method | Use Case | Returns | Typical SQL |
| --- | --- | --- | --- |
| ExecuteNonQuery | No result set | int (rows affected) | INSERT, UPDATE, DELETE |
| ExecuteScalar | Single value | object | COUNT, MAX, SUM |
| ExecuteReader | Multiple rows | [DataReader](https://www.mindstick.com/forum/2296/how-to-close-the-datareader) | SELECT |

## Key Differences at a Glance

| Aspect | NonQuery | Scalar | Reader |
| --- | --- | --- | --- |
| Returns data rows | No | No | Yes |
| Single value | No | Yes | No |
| Multiple rows | No | No | Yes |
| Performance | Fast | Very fast | Best for [large data](https://www.mindstick.com/interview/872/how-do-you-load-large-data-to-the-sqlserver-database) |

## Interview Tip (Important)

## Rule of thumb:

- Use **ExecuteNonQuery** → no data expected
- Use **ExecuteScalar** → exactly one value expected
- Use **ExecuteReader** → multiple rows expected


---

Original Source: https://answers.mindstick.com/qa/116279/explain-the-executenonquery-executescalar-and-executereader-methods-of-the-command-object

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
