Home >Database >Mysql Tutorial >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();
The above is the detailed content of How to Retrieve a Count from an SQL Query in C#?. For more information, please follow other related articles on the PHP Chinese website!