public bool addComment(Comments newComment) { bool flag; SqlConnection addCommentConnection = new SqlConnection("server=tf-PC\\SQLEXPRESS;database=blogData;uid=admin;pwd=s1y2x3"); addCommentConnection.Open(); string addString = "INSERT INTO Comments (commenterID,articleID,[content],commentTime) VALUES (@commenterID,@articleID,@content,@commentTime)"; SqlCommand addCommand = new SqlCommand(addString, addCommentConnection); addCommand.Parameters.Add(new SqlParameter("commenterID", SqlDbType.Int)); addCommand.Parameters["commenterID"].Value = newComment.commenterID; addCommand.Parameters.Add(new SqlParameter("articleID", SqlDbType.Int)); addCommand.Parameters["articleID"].Value = newComment.articleID; addCommand.Parameters.Add(new SqlParameter("content", SqlDbType.NVarChar, 255)); addCommand.Parameters["content"].Value = newComment.content; addCommand.Parameters.Add(new SqlParameter("commentTime", SqlDbType.DateTime)); addCommand.Parameters["commentTime"].Value = newComment.commentTime; if (addCommand.ExecuteNonQuery() > 0) { flag = true; } else { flag = false; } addCommentConnection.Close(); return flag; }
public IEnumerable<Comments> getAllCommentsByArticleID(int articleID) { SqlConnection loadConnection = new SqlConnection("server=tf-PC\\SQLEXPRESS;database=blogData;uid=admin;pwd=s1y2x3"); loadConnection.Open(); string loadArticlesCommentsString = "SELECT * FROM Comments WHERE articleID=@articleID"; SqlCommand loadCommand5 = new SqlCommand(loadArticlesCommentsString, loadConnection); loadCommand5.Parameters.Add(new SqlParameter("articleID", SqlDbType.Int)); loadCommand5.Parameters["articleID"].Value = articleID; SqlDataAdapter CommentAdapter = new SqlDataAdapter(loadCommand5); DataSet loadDataSet = new DataSet(); CommentAdapter.Fill(loadDataSet, "Comments"); Comments[] comments = new Comments[loadDataSet.Tables["Comments"].Rows.Count]; for (int i = 0; i < loadDataSet.Tables["Comments"].Rows.Count; i++) { comments[i] = new Comments() { ID = Convert.ToInt32(loadDataSet.Tables["Comments"].Rows[i]["ID"]), articleID = Convert.ToInt32(loadDataSet.Tables["Comments"].Rows[i]["articleID"]), commenterID = Convert.ToInt32(loadDataSet.Tables["Comments"].Rows[i]["commenterID"]), commentTime = Convert.ToDateTime(loadDataSet.Tables["Comments"].Rows[i]["commentTime"]), content = loadDataSet.Tables["Comments"].Rows[i]["content"].ToString() }; } return comments; }