I have two columns in my ForumPost Table: Id
and ThreadId
.
I want to count all Entries in Id
and ThreadId
for a specific User:
Code snippet
cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = @"SELECT dbo.ForumPost.ThreadId, "
+ "Id, "
+ " COUNT(ForumPost.Id)AS PostCount AND (ForumPost.ThreadId)AS
ThreadCount"
+ " FROM ForumPost "
+ " WHERE UserName = @UserName ";
cmd.Parameters.Add(new SqlParameter("@UserName", ThreadUserName));
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
int postCount = Convert.ToInt16(reader["PostCount"]);
int threadCount = Convert.ToInt16(reader["ThreadCount"]);
AllPosts = postCount + threadCount;
}
}
reader.Close();