Exemplo n.º 1
0
        public override bool InsertSinger(SingerModel singer)
        {
            bool existSinger           = false;
            bool inserted              = !existSinger;
            List <SingerModel> singers = GetSingers();

            try
            {
                existSinger = singers.Any(x => x.Name == singer.Name);
                if (!existSinger)
                {
                    using (SqlConnection connection = Conection.GetConection())
                    {
                        string     query      = "insert into singersTable values (@name, @kindOfMusic, @songs)";
                        SqlCommand sqlCommand = new SqlCommand(query, connection);
                        sqlCommand.Parameters.AddWithValue("@name", singer.Name);
                        sqlCommand.Parameters.AddWithValue("@kindOfMusic", singer.KindOfMusic);
                        sqlCommand.Parameters.AddWithValue("@songs", string.Join(",", singer.Songs));
                        connection.Open();
                        sqlCommand.ExecuteNonQuery();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(inserted);
        }
Exemplo n.º 2
0
        public override List <SingerModel> GetSingersByKindOfMusic(string kindOfMusic)
        {
            List <SingerModel> singers = new List <SingerModel>();

            try
            {
                using (SqlConnection connection = Conection.GetConection())
                {
                    string     query      = "select * from singersTable where kindOfMusic = @kindOfMusic";
                    SqlCommand sqlCommand = new SqlCommand(query, connection);
                    sqlCommand.Parameters.AddWithValue("@kindOfMusic", kindOfMusic);
                    connection.Open();
                    SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
                    while (sqlDataReader.Read())
                    {
                        singers.Add(new SingerModel
                        {
                            Name        = sqlDataReader.GetString(0),
                            KindOfMusic = sqlDataReader.GetString(1),
                            Songs       = sqlDataReader.GetString(0).Split(',').ToList()
                        });
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(singers);
        }