public string getStudentInfo() { string res = ""; StudentDao studentDao = new StudentDao(); MajorDao majorDao = new MajorDao(); ProfessorDao professorDao = new ProfessorDao(); List <Student> students = studentDao.listAllStudent(); List <AdminStudent> adminStudents = new List <AdminStudent>(); List <Major> majors = majorDao.listAllMajor(); if (students == null) { return(res); } else { foreach (Student s in students) { AdminStudent Astudent = new AdminStudent(); Astudent.id = s.id; Astudent.StuName = s.name; Astudent.major = majorDao.getMajorById(s.majorId).name; //专业方向? Astudent.infoCommited = s.infoChecked; if (s.firstWill != null && s.secondWill != null) { Astudent.twoWillCommited = true; } else { Astudent.twoWillCommited = false; } if (s.firstWillState == 1) { Astudent.FinalTutor = professorDao.getProfessorById(s.firstWill).name; } else if (s.secondWillState == 1) { Astudent.FinalTutor = professorDao.getProfessorById(s.secondWill).name; } else if (s.dispensedWill == null || s.dispensedWill == "") { Astudent.FinalTutor = null; } else { Astudent.FinalTutor = professorDao.getProfessorById(s.dispensedWill).name; } adminStudents.Add(Astudent); } JavaScriptSerializer serializer = new JavaScriptSerializer(); var json = serializer.Serialize(adminStudents); res = json.ToString(); serializer = null; } return(res); }
public string getAllMajor2() { try { MajorDao majorDao = new MajorDao(); List <Major> majors = majorDao.listAllMajor(); JavaScriptSerializer serializer = new JavaScriptSerializer(); var json = serializer.Serialize(majors); return(json); } catch (Exception e) { return("fail:查询失败!"); } }
public string getAllMajor() { try { MajorDao majorDao = new MajorDao(); List <Major> majors = majorDao.listAllMajor(); JavaScriptSerializer serializer = new JavaScriptSerializer(); var json = serializer.Serialize(majors); string retStr = json.ToString(); return(retStr); } catch (Exception e) { LogUtil.writeLogToFile(e, Request); return("fail:查询失败!"); } }
public string batchAddStudents(HttpPostedFileBase file) { var severPath = this.Server.MapPath("/ExcelFiles/"); if (!Directory.Exists(severPath)) { Directory.CreateDirectory(severPath); } var savePath = Path.Combine(severPath, file.FileName); Student student = null; StudentDao studentDao = new StudentDao(); string result = "{}"; bool flag = false; int addres = 0; List <Student> listStudents = new List <Student>(); Workbook workbook = new Workbook(); Worksheet sheet = null; Response.ContentType = "application/json"; Response.Charset = "utf-8"; try { if (string.Empty.Equals(file.FileName) || (".xls" != Path.GetExtension(file.FileName) && ".xlsx" != Path.GetExtension(file.FileName))) { throw new Exception("文件格式不正确"); } file.SaveAs(savePath); workbook.LoadFromFile(savePath); sheet = workbook.Worksheets[0]; int row = sheet.Rows.Length; //获取不为空的行数 int col = sheet.Columns.Length; //获取不为空的列数 string tempId; string tempName; string tempPasswd; string tempMajor; string tempGender; int idcol = -11; int namecol = -11; int Passwdcol = -11; int Majorcol = -11; int Gendercol = -11; int idrow = -11; //int maxnumcol = -11; CellRange[] cellrange = sheet.Cells; int rangelength = cellrange.Length; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) { tempId = cellrange[i * col + j].Value; if (tempId.Equals("学号")) { idcol = j; idrow = i + 1; } else if (tempId.Equals("姓名")) { namecol = j; } else if (tempId.Equals("密码")) { Passwdcol = j; } else if (tempId.Equals("专业方向")) { Majorcol = j; } else if (tempId.Equals("性别")) { Gendercol = j; } } if (idcol >= 0 && namecol >= 0) { break; } } if (idcol < 0 || namecol < 0) { throw new Exception("表格格式不正确"); } MajorDao majorDao = new MajorDao(); List <Major> majors = majorDao.listAllMajor(); for (int i = idrow; i < row; i++) { tempId = cellrange[i * col + idcol].Value; tempName = cellrange[i * col + namecol].Value; tempPasswd = cellrange[i * col + Passwdcol].Value; tempMajor = cellrange[i * col + Majorcol].Value; tempGender = cellrange[i * col + Gendercol].Value; if (tempName != "") { if (studentDao.getStudentById(tempId) != null) { flag = true; result += "已存在教师:id:" + tempId + " 姓名:" + tempName + " 专业:" + tempId + "\n"; continue; } student = new Student(); student.id = tempId; student.name = tempName; bool majorExist = false; foreach (Major m in majors) { if (m.name == tempMajor) { student.majorId = m.id; majorExist = true; } } if (!majorExist) { result += "无法识别名为‘" + tempMajor + "’的专业名称:id:" + tempId + " 姓名:" + tempName + " 专业:" + tempId + "\n"; flag = true; continue; } student.password = CryptoUtil.Md5Hash(tempPasswd); student.gender = tempGender == "女" ? false : true; addres = studentDao.addStudent(student); if (addres < 1) { throw new Exception("数据库连接出错"); } } } } catch (Exception e) { LogUtil.writeLogToFile(e, Request); return("{\"error\":\"" + e.Message + "\"}"); } finally { workbook.Dispose(); sheet = null; workbook = null; } if (flag) { return("{\"error\":\"" + result + "\"}"); } return("{" + "\"initialPreview\":" + "[\"<div style=\\\"text-align:center;padding:50px 25px;color:#00a65a\\\"><i class=\\\"fa fa-check-square-o\\\" style=\\\"font-size:60px;opacity:0.6\\\"></i><p style=\\\"padding-top:10px;font-size:18px\\\">添加成功</p></div>\"]" + "}"); }