Retrieving the number of records returned by a SQL query into an integer variable in C# can be achieved with ease.
Solution:
The most straightforward approach involves utilizing the SqlCommand.ExecuteScalar() method. This method executes the SQL command and returns a single value as the output. To capture the count returned by the COUNT(*) expression, simply cast the result to an integer:
cmd.CommandText = "SELECT COUNT(*) FROM table_name"; int count = (Int32)cmd.ExecuteScalar();
This code snippet assigns the integer representing the record count to the count variable, providing developers with a convenient way to work with the result of the SQL query.
The above is the detailed content of How to Get the Record Count from a SQL Query in C#?. For more information, please follow other related articles on the PHP Chinese website!