Beispiel #1
0
        public Countrie Get(string Country)
        {
            Countrie data = null;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();

                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"SELECT * FROM Countries WHERE CountryName = @CountryName";
                cmd.CommandType = CommandType.Text;
                cmd.Connection  = connection;
                cmd.Parameters.AddWithValue("@CountryName", Country);

                using (SqlDataReader dbReader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (dbReader.Read())
                    {
                        data = new Countrie()
                        {
                            CountryName = Convert.ToString(dbReader["CountryName"])
                        };
                    }
                }

                connection.Close();
            }
            return(data);
        }
Beispiel #2
0
        public void ReadAdressFile()
        {
            string text = System.IO.File.ReadAllText(@"C:\Users\GodOfWar\Desktop\db.sql");

            text = text.Replace("\n", "");
            text = text.Replace("'", "");
            string[] CRC = text.Split(new string[] { "INSERT INTO" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < CRC.Length; i++)
            {
                CRC[i] = CRC[i].Split(new string[] { "VALUES" }, StringSplitOptions.RemoveEmptyEntries)[1];
            }

            using (var context = new EstablishmentContext())
            {
                string[] cities = CRC[0].Split(new string[] { ")," }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < cities.Length; i++)
                {
                    string[] element = cities[i].Split(new string[] { "," }, StringSplitOptions.None);
                    element[0] = element[0].Replace("(", "");
                    City city = new City()
                    {
                        Id_City    = int.Parse(element[0].Replace(" ", "")),
                        Id_Region  = int.Parse(element[1].Replace(" ", "")),
                        Id_Country = int.Parse(element[2].Replace(" ", "")),
                        Name       = element[3] != "" ? element[3] : "--"
                    };
                    context.Cityes.Add(city);
                }
                string[] countries = CRC[1].Split(new string[] { ")," }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < countries.Length; i++)
                {
                    string[] element = countries[i].Split(new string[] { "," }, StringSplitOptions.None);
                    element[0] = element[0].Replace("(", "");
                    Countrie countrie = new Countrie()
                    {
                        Id_Country = int.Parse(element[0].Replace(" ", "")),
                        Name       = element[1] != "" ? element[1] : "--"
                    };
                    context.Countries.Add(countrie);
                }
                string[] regions = CRC[2].Split(new string[] { ")," }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < regions.Length; i++)
                {
                    string[] element = regions[i].Split(new string[] { "," }, StringSplitOptions.None);
                    element[0] = element[0].Replace("(", "");
                    Region region = new Region()
                    {
                        Id_Region  = int.Parse(element[0].Replace(" ", "")),
                        Id_Country = int.Parse(element[1].Replace(" ", "")),
                        Name       = element[2] != "" ? element[2] : "--"
                    };
                    context.Regions.Add(region);
                }
                context.SaveChanges();
            }
        }
Beispiel #3
0
 public ActionResult Input(string id = "")
 {
     if (string.IsNullOrEmpty(id))
     {
         ViewBag.Title      = "Create new country";
         ViewBag.SmallTitle = "Thêm Quốc Gia";
         Countrie newCountry = new Countrie()
         {
             CountryName = ""
         };
         return(View(newCountry));
     }
     else
     {
         ViewBag.Title      = "Edit a country";
         ViewBag.SmallTitle = "Sửa Quốc Gia";
         Countrie editCountry = CatalogBLL.Get(id);
         return(View(editCountry));
     }
 }
Beispiel #4
0
        public int Add(Countrie data)
        {
            int countryId = 0;

            using (SqlConnection connection = new SqlConnection(this.connectionString))
            {
                connection.Open();

                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"INSERT INTO Countries (CountryName) VALUES(@CountryName)";
                cmd.CommandType = CommandType.Text;
                cmd.Connection  = connection;
                cmd.Parameters.AddWithValue("@CountryName", data.CountryName);


                countryId = Convert.ToInt32(cmd.ExecuteScalar());

                connection.Close();
            }
            return(countryId);
        }
Beispiel #5
0
        public ActionResult Input(Countrie model, string method, string country)
        {
            if (string.IsNullOrEmpty(model.CountryName))
            {
                ModelState.AddModelError("CountryName", "CountryName Expected");
            }



            if (!ModelState.IsValid)
            {
                ViewBag.Title = model.CountryName == "" ? "Create new Country" : "Edit a Country";
                return(View(model));
            }

            if (method == "Edit a country")
            {
                CatalogBLL.Update(model, country);
            }
            else
            {
                if (CatalogBLL.Get(model.CountryName) != null)
                {
                    ModelState.AddModelError("CountryName", "CountryName Existed");
                    if (!ModelState.IsValid)
                    {
                        ViewBag.Title = model.CountryName == "" ? "Create new Country" : "Edit a Country";
                        return(View(model));
                    }
                }
                else
                {
                    CatalogBLL.Add(model);
                }
            }


            return(RedirectToAction("Index"));
        }
Beispiel #6
0
        public bool Update(Countrie data, string country)
        {
            int rowsAffected = 0;

            using (SqlConnection connection = new SqlConnection(this.connectionString))
            {
                connection.Open();

                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"UPDATE Countries
                                           SET CountryName = @CountryName                                                                                       
                                          WHERE CountryName = @country";
                cmd.CommandType = CommandType.Text;
                cmd.Connection  = connection;
                cmd.Parameters.AddWithValue("@CountryName", data.CountryName);
                cmd.Parameters.AddWithValue("@country", country);
                rowsAffected = Convert.ToInt32(cmd.ExecuteNonQuery());

                connection.Close();
            }

            return(rowsAffected > 0);
        }
 public static bool Update(Countrie data, string country)
 {
     return(CountrieDB.Update(data, country));
 }
 public static int Add(Countrie data)
 {
     return(CountrieDB.Add(data));
 }