Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How can we use sql server count function in Asp.net c# to count table records...
Posted

1 solution

Luckily, it's pretty easy:

C#
using (SqlConnection conn = new SqlConnection(connString))
{
    SqlCommand cmd = new SqlCommand("SELECT COUNT(*) FROM wherever", conn);
    conn.Open();
    int count  = (int)cmd.ExecuteScalar();
}


Obviously, you need to modify the SQL and provide connString, the usual exception handling is also missing from my sample.
 
Share this answer
 
Comments
RaisKazi 14-Nov-12 19:53pm    
My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900