private void button3_Click(object sender, RoutedEventArgs e) { CinemaDataContext Context = new CinemaDataContext(); int i = 0; string[] Strs = File.ReadAllLines("Films.txt"); Context.ExecuteCommand("delete from tblMovie"); do { tblMovie Movie = new tblMovie(); Movie.MovieName = Strs[i++]; Movie.MovieDirector = Strs[i++]; Movie.MovieDuration = Convert.ToInt32(Strs[i++]); Movie.MovieYear = Convert.ToInt32(Strs[i++]); Movie.MinAge = Convert.ToInt32(Strs[i++]); Movie.MainActor = Strs[i++]; Context.tblMovies.InsertOnSubmit(Movie); Context.SubmitChanges(); int MovieID = (from M in Context.tblMovies where M.MovieName == Movie.MovieName select M.MovieID).First(); string[] GenresStrs = Strs[i++].Split(); foreach (string S in GenresStrs) { tblMovieGenre MovieGenre = new tblMovieGenre(); MovieGenre.MovieID = MovieID; MovieGenre.GenreID = (from G in Context.tblGenres where G.GenreName == S select G.GenreID).First(); Context.tblMovieGenres.InsertOnSubmit(MovieGenre); } string[] CountriesStrs = Strs[i++].Split(); foreach (string S in CountriesStrs) { tblMovieCountry MovieCountry = new tblMovieCountry(); MovieCountry.MovieID = MovieID; MovieCountry.CountryID = (from G in Context.tblCountries where G.CountryName == S select G.CountryID).First(); Context.tblMovieCountries.InsertOnSubmit(MovieCountry); } i++; }while (i < Strs.Length); Context.SubmitChanges(); }
static public void Delete(int movieId, int genreId) { using (DVDEntities dc = new DVDEntities()) { tblMovieGenre movieGenre = dc.tblMovieGenres.FirstOrDefault(mg => mg.MovieId == movieId && mg.GenreId == genreId); if (movieGenre != null) { dc.tblMovieGenres.Remove(movieGenre); dc.SaveChanges(); } } }
public static void Add(int movieId, int genreId) { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblMovieGenre mg = new tblMovieGenre(); mg.Id = dc.tblMovieGenres.Any() ? dc.tblMovieGenres.Max(p => p.Id) + 1 : 1; mg.MovieId = movieId; mg.GenreId = genreId; dc.tblMovieGenres.Add(mg); dc.SaveChanges(); } }
public static void Delete(int movieId, int genreId) { using (DVDCentralEntities dc = new DVDCentralEntities()) { tblMovieGenre mg = dc.tblMovieGenres.FirstOrDefault(p => p.MovieId == movieId && p.GenreId == genreId); if (mg != null) { dc.tblMovieGenres.Remove(mg); dc.SaveChanges(); } } }
static public void Add(int movieId, int genreId) { using (DVDEntities dc = new DVDEntities()) { tblMovieGenre mg = new tblMovieGenre { Id = dc.tblMovieGenres.Any() ? dc.tblMovieGenres.Max(m => m.Id) + 1 : 1, MovieId = movieId, GenreId = genreId }; dc.tblMovieGenres.Add(mg); dc.SaveChanges(); } }
private void button3_Click(object sender, RoutedEventArgs e) { try { tblMovie Movie = new tblMovie(); if (string.IsNullOrWhiteSpace(tbFilmName.Text)) { throw new Exception("Не введено название фильма"); } Movie.MovieName = tbFilmName.Text; if (string.IsNullOrWhiteSpace(tbDirector.Text)) { throw new Exception("Не введено имя режиссера"); } Movie.MovieDirector = tbDirector.Text; Movie.MovieDuration = Convert.ToInt32(udDuration.Value); Movie.MovieYear = Convert.ToInt32(udYear.Value); Movie.MinAge = Convert.ToInt32(udMinAge.Value); if (string.IsNullOrWhiteSpace(tbMainHero.Text)) { throw new Exception("Не введено имя главного актера"); } Movie.MainActor = tbMainHero.Text; if (string.IsNullOrWhiteSpace(tbGenres.Text)) { throw new Exception("Не выбран хотя бы один жанр"); } if (string.IsNullOrWhiteSpace(tbCountries.Text)) { throw new Exception("Не выбран хотя бы одна страна"); } CinemaDataContext Context = new CinemaDataContext(); Context.tblMovies.InsertOnSubmit(Movie); Context.SubmitChanges(); int MovieID = (from M in Context.tblMovies where M.MovieName == tbFilmName.Text select M.MovieID).ToArray()[0]; List <int> GenresID; List <int> CountriesID; GetID(tbGenres.Text, tbCountries.Text, out GenresID, out CountriesID); foreach (int ID in GenresID) { tblMovieGenre MovieGenre = new tblMovieGenre(); MovieGenre.MovieID = MovieID; MovieGenre.GenreID = ID; Context.tblMovieGenres.InsertOnSubmit(MovieGenre); } foreach (int ID in CountriesID) { tblMovieCountry MovieCountry = new tblMovieCountry(); MovieCountry.MovieID = MovieID; MovieCountry.CountryID = ID; Context.tblMovieCountries.InsertOnSubmit(MovieCountry); } Context.SubmitChanges(); ReloadFilmsRooms(); MessageBox.Show("Фильм " + Movie.MovieName + " успешно добавлен"); } catch (Exception Ex) { MessageBox.Show("Ошибка: " + Ex.Message); } }