예제 #1
0
        public void LoadProgramFromCSV()
        {
            if (!(ds.Programs.Count() > 0))
            {
                // File system path to the data file (in this project's App_Data folder)
                string path = HttpContext.Current.Server.MapPath("~/App_Data/ict-programs.csv");

                // Create a stream reader object, to read from the file system
                StreamReader sr = File.OpenText(path);

                // Create the CsvHelper object
                var csv = new CsvReader(sr);

                // Configure the mapping classes (if it exists)
                csv.Configuration.RegisterClassMap <ProgramMap>();

                // Go through the data file
                while (csv.Read())
                {
                    // Read one line in the source file into a new object
                    ProgramAdd qb = csv.GetRecord <ProgramAdd>();

                    // Add the new object to the data store
                    ds.Programs.Add(Mapper.Map <Program>(qb));
                }

                ds.SaveChanges();

                // Clean up
                sr.Close();
                sr = null;
            }
        }
예제 #2
0
        //#####################################################################
        //Dataimports
        public IEnumerable <ProgramBase> ReadProgramsFromCSV()
        {
            // File system path to the data file (in this project's App_Data folder)
            string path = HttpContext.Current.Server.MapPath("~/App_Data/ict-programs.csv");

            // Create a stream reader object, to read from the file system
            StreamReader sr = File.OpenText(path);

            // Create the CsvHelper object
            var csv = new CsvReader(sr);

            // Configure the mapping classes (if it exists)
            csv.Configuration.RegisterClassMap <ProgramMap>();

            // Configure a collection to hold the results
            List <ProgramAdd> results = new List <ProgramAdd>();

            // Go through the data file
            while (csv.Read())
            {
                // Read one line in the source file
                ProgramAdd qb = csv.GetRecord <ProgramAdd>();

                results.Add(qb);
            }

            // Clean up
            sr.Close();
            sr = null;

            // Return the results
            return(Mapper.Map <IEnumerable <ProgramBase> >(results));
        }