Use SqlCommand.ExecuteScalar() and cast it to an int
:
cmd.CommandText = "SELECT COUNT(*) FROM table_name";
Int32 count = (Int32) cmd.ExecuteScalar();
More Related Contents:
- SQL Server Char/VarChar DateTime Error [closed]
- Delete from two tables? [closed]
- How to get last inserted id?
- What are the pros and cons to keeping SQL in Stored Procs versus Code [closed]
- Insert entire DataTable into database at once instead of row by row?
- Getting return value from stored procedure in C#
- SQL error: Incorrect syntax near the keyword ‘User’
- What represents a double in sql server?
- Passing List to SQL Stored Procedure
- SQL WHERE clause matching values with trailing spaces
- Query extremely slow in code but fast in SSMS
- How to retrieve data from a SQL Server database in C#?
- SqlBulkCopy from a List
- Any way to SQLBulkCopy “insert or update if exists”?
- SQLException : String or binary data would be truncated
- Multi threading C# application with SQL Server database calls
- What’s the fastest way to bulk insert a lot of data in SQL Server (C# client)
- SqlConnection.Close() inside using statement
- Can I use ADFS 2.0 to authenticate certain users against SQL Server?
- ExecuteNonQuery returning -1 when using sql COUNT despite the query string
- The variable name ‘@’ has already been declared. Variable names must be unique within a query batch or stored procedure
- Add WHERE clauses to SQL dynamically / programmatically
- How to pass string array in SQL parameter to IN clause in SQL
- How to pass a null variable to a SQL Stored Procedure from C#.net code
- How to insert data into SQL Server
- Query validation using C#
- Use SQL Server time datatype in C#.NET application?
- Database Error: There is no row at position 0
- Generating sql code programmatically
- How to give ADO.NET Parameters