What is ExecuteScalar in ADO.NET and when should you use it?
Updated Apr 22, 2026
Short answer
ExecuteScalar is used to retrieve a single value from the database, typically the first column of the first row in the result set.
Deep explanation
Purpose: Designed for queries that return a single value (e.g., COUNT, SUM, MAX) Performance: More efficient than ExecuteReader when only one value is needed Behavior: Ignores remaining rows/columns after the first value Common Use Cases: Getting record counts Fetching identity values Comparison: ExecuteReader → multiple rows ExecuteNonQuery → no return value ExecuteScalar → single value
Unlock with a Pro subscription to view this section.
View pricingReal-world example
No real-world example available yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProCommon mistakes
No common mistakes listed yet.
Unlock with a Pro subscription to view this section.
Upgrade to ProFollow-up questions
No follow-up questions available yet.
Unlock with a Pro subscription to view this section.
Upgrade to Pro