示例#1
0
        /// <summary>
        /// GetPostByID returns a post with the approved comment count.
        /// </summary>
        public Post GetPostById(int postId)
        {
            Collection <Post> posts = new Collection <Post>();

            using (SqlConnection connection = this.GetConnection())
            {
                connection.Open();

                SqlCommand sc = new SqlCommand();

                sc.CommandText = "GetPostById";

                sc.Parameters.AddWithValue("PostId", postId);

                sc.CommandType = CommandType.StoredProcedure;
                sc.Connection  = connection;

                using (SqlDataReader reader = sc.ExecuteReader())
                {
                    posts = SqlDataMap.CreatePostsFromReader(reader);
                }
            }

            if (posts.Count > 0)
            {
                return(posts[0]);
            }
            else
            {
                return(null);
            }
        }
示例#2
0
        public Collection <Post> GetAllPosts(int categoryId, DateTime startDate, DateTime endDate, CommentType ctype, int numberOfPosts)
        {
            //// int numberOfPosts = 10;
            Collection <Post> posts = new Collection <Post>();

            using (SqlConnection connection = this.GetConnection())
            {
                connection.Open();

                SqlCommand sc = new SqlCommand();

                sc.CommandText = "GetAllPosts";

                sc.Parameters.AddWithValue("NumberOfPosts", numberOfPosts);
                sc.Parameters.AddWithValue("CategoryId", categoryId);
                sc.Parameters.AddWithValue("DateFrom", startDate);
                sc.Parameters.AddWithValue("DateTo", endDate);

                if (ctype != CommentType.All)
                {
                    sc.Parameters.AddWithValue("@ApprovedCommentsOnly", ctype == CommentType.Approved ? 1 : 0);
                }
                else
                {
                    sc.Parameters.AddWithValue("@ApprovedCommentsOnly", DBNull.Value);
                }

                sc.CommandType = CommandType.StoredProcedure;
                sc.Connection  = connection;

                using (SqlDataReader reader = sc.ExecuteReader())
                {
                    posts = SqlDataMap.CreatePostsFromReader(reader);
                }
            }

            return(posts);
        }