Пример #1
0
 /// <summary>
 /// This method reads locations from a file, adds them to DbSet, and then saves changes to the database.
 /// </summary>
 public void AddLocations()
 {
     try
     {
         string[]      lines = File.ReadAllLines(@"../../Lokacije.txt");
         List <string> list  = new List <string>();
         for (int i = 0; i < lines.Length; i++)
         {
             tblLocation location = new tblLocation();
             list             = lines[i].Split(',').ToList();
             location.Address = list[0];
             location.City    = list[1];
             location.State   = list[2];
             using (Employee_DataEntities context = new Employee_DataEntities())
             {
                 //checking if location already exists
                 if (context.tblLocations.Where(x => x.Address == location.Address && x.City == location.City && x.State == location.State).ToList().Count == 0)
                 {
                     //if not exists, adding location to DbSet and saving changes to database
                     context.tblLocations.Add(location);
                     context.SaveChanges();
                     logger.LogAction("Location " + location.Address + ", " + location.City + ", " + location.State + " created.");
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception" + ex.Message.ToString());
     }
 }
        // This method reads locations from file, adds them to database
        public void AddLocations()
        {
            int id = 0;

            using (EmployeeDBEntities context = new EmployeeDBEntities())
            {
                if (File.Exists(@"..\..\Locations.txt"))
                {
                    string[] readFile = File.ReadAllLines(@"..\..\Locations.txt");

                    for (int i = 0; i < readFile.Length; i++)
                    {
                        if (!string.IsNullOrEmpty(readFile[i]))
                        {
                            string[] trim    = readFile[i].Split(',');
                            string   address = trim[0];
                            string   city    = trim[1];
                            string   country = trim[2];
                            id++;

                            tblLocation loc = new tblLocation()
                            {
                                LocationID = id,
                                Address    = address,
                                City       = city,
                                State      = country
                            };
                            context.tblLocations.Add(loc);
                            context.SaveChanges();
                        }
                    }
                }
            }
        }