public bool ArticleCommentCreateDeleteUpdate(ArticleComment articleComment, UserAction ua) { bool result = false; string commandText = string.Empty; switch (ua) { case UserAction.Create: commandText = "INSERT INTO ArticleComment (ArticleCommentID, ArticleCommentText, UserID, UserName, UserAddress, ArticleCommentCreateTime, ArticleID) VALUES ("+ articleComment.CommentID +", '"+articleComment.CommentText +"',"+articleComment.UserID +", '"+articleComment.UserName +"', '"+articleComment.UserAddress +"','"+articleComment.CommentCreateTime +"',"+articleComment.ArticleID +")"; break; case UserAction.Delete: commandText = "DELETE FROM ArticleComment WHERE (ArticleCommentID = "+articleComment.CommentID +")"; break; case UserAction.Update: commandText = "UPDATE ArticleComment SET ArticleCommentText = '"+articleComment.CommentText +"', UserID = "+articleComment.UserID +", UserName = '******', UserAddress = '"+articleComment.UserAddress + "',ArticleCommentCreateTime = '"+articleComment.CommentCreateTime +"', ArticleID = "+articleComment.ArticleID +" WHERE (ArticleCommentID = "+articleComment.CommentID +")"; break; } using (SqlConnection conn = new SqlConnection(DataHelper2.SqlConnectionString)) { using (SqlCommand comm = new SqlCommand()) { comm.CommandText = commandText; comm.Connection = conn; conn.Open(); try { comm.ExecuteNonQuery(); result = true; } catch (Exception ex) { throw new Exception(ex.Message); } } } return result; }
public List<ArticleComment> GetArticleCommentsByUserID(int userID, int count, OrderKey ok) { List<ArticleComment> list = new List<ArticleComment>(); string orderKey = " order by "; switch (ok) { //case OrderKey.Good: // orderKey = "IsGood desc"; // break; case OrderKey.ID: orderKey = "ArticleID desc"; break; case OrderKey.Time: orderKey = "ArticleCommentCreateTime desc"; break; default: orderKey = "ArticleCommentID desc"; break; } string commText = string.Empty; switch (count) { case 0: commText = "SELECT ArticleCommentText, UserID, UserName, UserAddress, ArticleCommentCreateTime, ArticleID, ArticleCommentID " + " FROM ArticleComment " + " WHERE (UserID = " + userID + ") "+ orderKey ; break; default: commText = "select top " + count.ToString() + " * from ArticleComment WHERE (UserID = " + userID + ")" + orderKey ; break; } using (SqlConnection conn = new SqlConnection(DataHelper2.SqlConnectionString)) { { using (SqlCommand comm = new SqlCommand()) { comm.Connection = conn; comm.CommandText = commText; conn.Open(); using (SqlDataReader reader = comm.ExecuteReader()) { while (reader.Read()) { ArticleComment comment = new ArticleComment(); comment.CommentID = int.Parse(reader["ArticleCommentID"].ToString()); comment.ArticleID = int.Parse(reader["ArticleID"].ToString()); comment.CommentCreateTime = DateTime.Parse(reader["ArticleCommentCreateTime"].ToString()); comment.CommentText = reader["ArticleCommentText"].ToString(); comment.UserAddress = reader["UserAddress"].ToString(); comment.UserID = int.Parse(reader["UserID"].ToString()); comment.UserName = reader["UserName"].ToString(); list.Add(comment); } } } } } return list; }