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();
755k questions
547k answers
4 comments
86.4k users