public IList <ForumPostComments> GetAllCommentsByPost(ForumPost forumPost) { List <ForumPostComments> commentsList = new List <ForumPostComments>(); try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); string sql = $"SELECT * FROM Forum_Post_Comment Where postId = @id order by id desc"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@id", forumPost.Id); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { ForumPostComments comment = new ForumPostComments(); comment.Id = Convert.ToInt32(reader["id"]); comment.Message = Convert.ToString(reader["message"]); comment.User = Convert.ToString(reader["username"]); commentsList.Add(comment); } } } catch (SqlException ex) { throw ex; } return(commentsList); }
public void SaveComment(ForumPostComments comment) { try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); string sql = $"insert into Forum_Post_Comment (username, message, postId) values (@username, @message, @postId);"; SqlCommand cmd = new SqlCommand(sql, conn); cmd.Parameters.AddWithValue("@username", comment.User); cmd.Parameters.AddWithValue("@message", comment.Message); cmd.Parameters.AddWithValue("@postId", comment.PostId); cmd.ExecuteScalar(); } } catch (SqlException ex) { throw ex; } }