Example #1
0
        public IEnumerable<Comment> GetCommentsForPost(int postId)
        {
            List<Comment> comments = new List<Comment>();
            using (var connection = new SqlConnection(_connectionString))
            using (var cmd = connection.CreateCommand())
            {
                cmd.CommandText = "SELECT * FROM Comments WHERE PostId = @postId";
                cmd.Parameters.AddWithValue("@postId", postId);
                connection.Open();
                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Comment comment = new Comment
                    {
                        Content = (string)reader["Content"],
                        Date = (DateTime)reader["Date"],
                        Id = (int)reader["Id"],
                        Name = (string)reader["Name"],
                        PostId = (int)reader["PostId"]
                    };
                    comments.Add(comment);
                }
            }

            return comments;
        }
Example #2
0
 public void AddComment(Comment comment)
 {
     using (var connection = new SqlConnection(_connectionString))
     using (var cmd = connection.CreateCommand())
     {
         cmd.CommandText = "INSERT INTO Comments (Content, Date, Name, PostId) VALUES (@content, @date, @name, @postId); SELECT @@Identity";
         cmd.Parameters.AddWithValue("@content", comment.Content);
         cmd.Parameters.AddWithValue("@date", comment.Date);
         cmd.Parameters.AddWithValue("@name", comment.Name);
         cmd.Parameters.AddWithValue("@postId", comment.PostId);
         connection.Open();
         comment.Id = (int)(decimal)cmd.ExecuteScalar();
     }
 }
Example #3
0
        public void AddComment(Comment comment)
        {
            using (var connection = new SqlConnection(_connectionString))
            using (var cmd = connection.CreateCommand())
            {
                cmd.CommandText = "INSERT INTO Comments (Name, Text, Date, PostId)" +
                                  " VALUES (@name, @text, @date, @postId)";
                cmd.Parameters.AddWithValue("@name", comment.Name);
                cmd.Parameters.AddWithValue("@text", comment.Text);
                cmd.Parameters.AddWithValue("@date", comment.Date);
                cmd.Parameters.AddWithValue("@postId", comment.PostId);

                connection.Open();
                cmd.ExecuteNonQuery();
            }
        }