What is the simplest way in C# (.cs file) to get the count from the SQL command
SELECT COUNT(*) FROM table_name
into an int variable?
int
Use SqlCommand.ExecuteScalar() and cast it to an int:
cmd.CommandText = "SELECT COUNT(*) FROM table_name"; Int32 count = (Int32) cmd.ExecuteScalar();
2.1m questions
2.1m answers
60 comments
57.0k users