Home >Database >Mysql Tutorial >How to Retrieve a Count from an SQL Query in C#?

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

DDD
DDDOriginal
2024-12-28 21:41:18983browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn