コード例 #1
0
        public IEnumerable <IRelease> GetReleasesToSearch(string searchQuery)
        {
            string[]        words    = searchQuery.Split(' ', StringSplitOptions.RemoveEmptyEntries);
            List <IRelease> toReturn = new List <IRelease>();

            using (SqlConnection conn = new SqlConnection(connectionstring)) {
                using (SqlCommand cmd = new SqlCommand(@"GetReleasesToSearch", conn)) {
                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter searchWord = new SqlParameter("@searchWord", SqlDbType.VarChar, 200);
                    conn.Open();
                    foreach (string word in words)
                    {
                        searchWord.Value = word;
                        if (!cmd.Parameters.Contains(searchWord))
                        {
                            cmd.Parameters.Add(searchWord);
                        }
                        cmd.Prepare();
                        using (SqlDataReader reader = cmd.ExecuteReader()) {
                            while (reader.Read())
                            {
                                IRelease tempRelease = new ReleaseDto
                                {
                                    Category = new CategoryDto
                                    {
                                        Name = reader["category"].ToString()
                                    },
                                    Title       = reader["title"].ToString(),
                                    Description = reader["description"].ToString(),
                                    Id          = Convert.ToInt32(reader["id"])
                                };
                                toReturn.Add(tempRelease);
                            }
                        }
                    }
                }
                // Removes duplicates from the list
                toReturn = toReturn.GroupBy(x => x.Id).Select(g => g.FirstOrDefault()).ToList();
                return(toReturn);
            }
        }
コード例 #2
0
        public List <IRelease> GetAll()
        {
            List <IRelease> toReturn = new List <IRelease>();

            using (SqlConnection conn = new SqlConnection(connectionstring)) {
                using (SqlCommand cmd = new SqlCommand(@"GetAllReleases", conn)) {
                    cmd.CommandType = CommandType.StoredProcedure;
                    conn.Open();
                    using (SqlDataReader reader = cmd.ExecuteReader()) {
                        while (reader.Read())
                        {
                            IRelease release = new ReleaseDto
                            {
                                Title = reader["releaseName"].ToString(),

                                FollowerCount = (int)reader["followCount"],
                                ReleaseDate   = Convert.ToDateTime(reader["releaseDate"]),
                                CreationDate  = Convert.ToDateTime(reader["creationDate"]),
                                Id            = (int)reader["releaseId"],
                                Description   = reader["description"].ToString(),
                                ImgLocation   = reader["ReleaseImage"].ToString(),
                                User          = new UserDto
                                {
                                    ImgLocation = reader["userImage"].ToString(),
                                    Username    = reader["username"].ToString()
                                },
                                Category = new CategoryDto
                                {
                                    ImgLocation = reader["categoryImage"].ToString(),
                                    Name        = reader["CategoryName"].ToString()
                                }
                            };
                            toReturn.Add(release);
                        }
                    }
                }
            }
            return(toReturn);
        }