Esempio n. 1
0
        void LoadDB()
        {
            try
            {
                List <string[]> data = new List <string[]>();
                using (var context = new CountryDBContext())
                {
                    foreach (CountryDb countries in context.countries.Include(c => c.city).Include(r => r.region))
                    {
                        data.Add(new string[] { countries.name, countries.code, countries?.city.name, countries.area.ToString(), countries.population.ToString(), countries?.region.name });
                    }
                }

                foreach (string[] s in data)
                {
                    this.dataGridView.Rows.Add(s);
                }
            }
            catch
            {
                MessageBox.Show("Ошибка в загрузке данных");
            }
        }
Esempio n. 2
0
        void WriteinDB(Country rc)
        {
            try
            {
                using (var context = new CountryDBContext())
                {
                    var  addcity  = new City();
                    City FindCity = context.cities.FirstOrDefault(c => c.name == rc.Capital);
                    if (FindCity == null)
                    {
                        Console.WriteLine("нет такого города");
                        Console.ReadLine();
                        addcity.name = rc.Capital;
                        context.cities.Add(addcity);
                    }
                    else
                    {
                        Console.WriteLine("есть город");
                        addcity.id = FindCity.id;
                    }
                    context.SaveChanges();



                    var    addregion  = new Region();
                    Region FindRegion = context.regions.FirstOrDefault(r => r.name == rc.Region);
                    if (FindRegion == null)
                    {
                        Console.WriteLine("нет такого региона");
                        Console.ReadLine();
                        addregion.name = rc.Region;
                        context.regions.Add(addregion);
                    }
                    else
                    {
                        Console.WriteLine("есть регион");
                        addregion.id = FindRegion.id;
                    }
                    context.SaveChanges();


                    var       usercountry  = new CountryDb();
                    CountryDb CheckCountry = context.countries.FirstOrDefault(c => c.code == rc.Alpha2Code);
                    if (CheckCountry == null)
                    {
                        usercountry.name       = rc.Name;
                        usercountry.code       = rc.Alpha2Code;
                        usercountry.area       = (float)rc.Area;
                        usercountry.population = rc.Population;
                        usercountry.cityId     = addcity.id;
                        usercountry.regionId   = addregion.id;
                        context.countries.Add(usercountry);
                    }
                    else
                    {
                        usercountry.id         = CheckCountry.id;
                        usercountry.name       = rc.Name;
                        usercountry.code       = rc.Alpha2Code;
                        usercountry.area       = (float)rc.Area;
                        usercountry.population = rc.Population;
                        usercountry.cityId     = addcity.id;
                        usercountry.regionId   = addregion.id;
                    }
                    context.SaveChanges();
                    this.dataGridView.Rows.Clear();
                    MessageBox.Show("Сохранение прошло успешно");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка при сохранении: " + ex.Message);
            }
        }