Ejemplo n.º 1
0
        public void ImportOverview(string[] headerNames, string filePath)
        {
            CsvToDictionaryLoader loader = new CsvToDictionaryLoader(headerNames);
            string csvFile = File.ReadAllText(@filePath);
            List <Dictionary <string, string> > overviews = loader.LoadToDictionary(csvFile);

            using (var context = new SossusvleiSedimentologyContext())
            {
                string idHeader = overviews.First().Keys.First();

                foreach (var sample in overviews)
                {
                    SampleOverview databaseEntry = new SampleOverview();
                    databaseEntry.SampleName = sample["SampleName"];
                    databaseEntry.Latitude   = sample["Latitude"].Substring(0, sample["Latitude"].Length - 1);
                    databaseEntry.Longitude  = sample["Longitude"].Substring(0, sample["Longitude"].Length - 1);
                    databaseEntry.Section    = sample["Section"];
                    databaseEntry.Facies     = sample["Facies"];

                    context.SampleOverview.Add(databaseEntry);

                    try
                    {
                        context.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        e.HandleSaveChangesSqlException(idHeader);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void Import()
        {
            CsvToDictionaryLoader loader = new CsvToDictionaryLoader(_headerNames);
            string csvFile = File.ReadAllText(_filePath);
            List <Dictionary <string, string> > maps = loader.LoadToDictionary(csvFile);

            using (var context = new SossusvleiSedimentologyContext())
            {
                string idHeader = maps.First().Keys.First();

                foreach (var map in maps)
                {
                    string         sampleName = map[idHeader];
                    SampleOverview overview   = context.SampleOverview.First(s => s.SampleName == sampleName);

                    if (overview is null)
                    {
                        Console.WriteLine($"Missing overview for sample '{sampleName}'. Skipping data import.");

                        continue;
                    }

                    var filteredMap = map.Where(m => m.Key != idHeader);

                    foreach (var item in filteredMap)
                    {
                        TSedimentologicalData data = _mapConverter.Convert(item, overview);
                        context.Set <TSedimentologicalData>().Add(data);

                        try
                        {
                            context.SaveChanges();
                        }
                        catch (Exception e)
                        {
                            e.HandleSaveChangesSqlException(map[idHeader]);
                        }
                    }
                }
            }
        }