"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Retrieve a Count from an SQL Query in C#?

How to Retrieve a Count from an SQL Query in C#?

Published on 2025-01-31
Browse:153

How to Retrieve a Count from an SQL Query in C#?

Retrieving Count from an SQL Query

In C#, capturing the count returned by an SQL query into an integer variable is a simple task. Here's how to do it:

Utilize SqlCommand.ExecuteScalar() to retrieve the scalar value from the query and cast it to an int data type:

cmd.CommandText = "SELECT COUNT(*) FROM table_name";
int count = (int)cmd.ExecuteScalar();
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3