private void AddStudentsToDB(string path) { FileStream stream = System.IO.File.Open(path, FileMode.Open, FileAccess.Read); IExcelDataReader excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream); DataSet result = excelReader.AsDataSet(); List <Student> students = new List <Student>(); int tablesCount = result.Tables.Count; for (int i = 0; i < tablesCount; i++) { string tableName = result.Tables[i].TableName; var parsed = ParseClass(tableName); int id = DB.GetClassId(parsed[0], parsed[1]); if (id == 0) { continue; } int row = 4; int col = 1; while (true) { string number = result.Tables[i].Rows[row][col].ToString(); if (number == String.Empty) { break; } string firstName = result.Tables[i].Rows[row][col + 1].ToString(); string secondName = result.Tables[i].Rows[row][col + 2].ToString(); string lastName = result.Tables[i].Rows[row][col + 3].ToString(); string egn = result.Tables[i].Rows[row][col + 4].ToString(); if (egn.Length < 10) { egn = (new string('0', 10 - egn.Length)) + egn; } string fullName = firstName + " " + secondName + " " + lastName; Student s = new Student() { Name = fullName, Number = Int32.Parse(number), EGN = egn, Class_id = id }; students.Add(s); row++; } } StudentsRepository.DeleteAllStudents(); StudentsRepository.ImportStudents(students); excelReader.Close(); }