title: How to count number of rows in SQL Server database?
body: <p>I am trying to count the number of unread messages in my DB Table but is proving to be very difficult. I've even read tutorials online but to no avail.</p>
  
  <p>What I'm doing should be simple.</p>
  
  <p>Here's what I'm trying to do:</p>
  
  <pre><code>COUNT NUMBER OF ROWS IN NOTIFICATIONSTABLE 
  WHERE USERID = @0 AND MESSAGEWASREAD = FALSE
  </code></pre>
  
  <p>Can somebody please point me in the <em>right</em> direction? Any help will be appreciated.</p>
  
  <p>Thank you</p>
  
  <pre><code>@helper RetrievePhotoWithName(int userid) 
  { 
      var database = Database.Open("SC"); 
      var name = database.QuerySingle("select FirstName, LastName, ProfilePicture from UserProfile where UserId = @0", userid); 
      var notifications =  database.Query("SELECT COUNT(*) as 'counter' FROM Notifications WHERE UserID = @0 AND [Read] = @1", userid, false); 
  
  var DisplayName = ""; 
  if(notifications["counter"] &lt; 1) 
  { 
      DisplayName = name["FirstName"] + " " + name["LastName"]; 
  } 
  else 
  { 
      DisplayName = name["FirstName"] + ", you have " + notifications["counter"] + " new messages."; 
  } 
  &lt;a href="@Href("~/Home")" title="My Account"&gt;&lt;img src="@Href("~/Shared/Assets/Images/" + name["ProfilePicture"] + ".png")" id="MiniProfilePicture" /&gt;&amp;nbsp;@DisplayName&lt;/a&gt; 
  
  database.Close(); 
  </code></pre>
  
  <p>} </p>