コード例 #1
0
        public void LoadCourseFromCSV()
        {
            if (!(ds.Courses.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-courses-bsd.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 <CourseMap>();

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

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

                ds.SaveChanges();

                // Clean up
                sr.Close();
                sr = null;
            }
        }
コード例 #2
0
        public IEnumerable <CourseBase> ReadCoursesFromCSV()
        {
            // File system path to the data file (in this project's App_Data folder)
            string path = HttpContext.Current.Server.MapPath("~/App_Data/ict-courses-bsd.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 <CourseMap>();

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

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

                results.Add(qb);
            }

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

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