excel导入数据到sqlserver

一,ADO.net的方式

1、读取excel数据到dataset

public static System.Data.DataSet ExcelSqlConnection(string filepath, string tableName){string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";OleDbConnection ExcelConn = new OleDbConnection(strCon);try{string strCom = string.Format("SELECT * FROM [Sheet1$]");ExcelConn.Open();OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, ExcelConn);DataSet ds = new DataSet();myCommand.Fill(ds, "[" + tableName + "$]");ExcelConn.Close();return ds;}catch{ExcelConn.Close();return null;}}

2、将数据写入到数据库

protected void Button2_Click(object sender, EventArgs e){string filename = FileUpload1.FileName;string savePath = Server.MapPath(("~/fujian/") + filename);FileUpload1.SaveAs(savePath);DataSet ds = ExcelSqlConnection(savePath, filename);//GridView1.DataSource = ds;//GridView1.DataBind();DataRow[] dr = ds.Tables[0].Select();for (int i = 0; i < dr.Length; i++){SqlConnection myconn = myconnect();myconn.Open();string title = dr[i]["名称"].ToString();string huanxianweizhi = dr[i]["环线位置"].ToString();string quyu = dr[i]["区域"].ToString();string sqlstr1 = "select * from dbo.test where name='" + quyu + "'";    //区域中间表转换SqlCommand myCmd1 = new SqlCommand(sqlstr1, myconn);SqlDataAdapter mydata = new SqlDataAdapter(myCmd1);DataSet my = new DataSet();mydata.Fill(my);GridView1.DataSource = my;GridView1.DataBind();string quyu1 = my.Tables[0].Rows[0][0].ToString(); ;//SqlDataAdapter adapt = new SqlDataAdapter(sqlstr1, myconn);DataSet ds1 = new DataSet();string zuoluo = dr[i]["座落"].ToString();string yongtu = dr[i]["用途"].ToString();string chengjiaotaoshu = dr[i]["成交套数"].ToString();string jianzhumianji = dr[i]["建筑面积"].ToString();string chengjiaozongjia = dr[i]["成交总价"].ToString();string dangrijunjia = dr[i]["当日均价"].ToString();string chengjiaoriqi = dr[i]["成交日期"].ToString();string qitashuoming = dr[i]["其他说明"].ToString();string bankuai = dr[i]["板块"].ToString();//SqlConnection myconn = myconnect();//myconn.Open();string sqlstr = "insert into dbo.youweishuju(名称,环线位置,区域,座落,用途,成交套数,建筑面积,成交总价,当日均价,成交日期,其他说明,板块)values('" + title + "','" + huanxianweizhi + "','" + quyu1 + "','" + zuoluo + "','" + yongtu + "','" + chengjiaotaoshu + "','"+ jianzhumianji +"','"+ chengjiaozongjia +"','"+ dangrijunjia +"','"+ chengjiaoriqi +"','"+ qitashuoming +"','"+ bankuai +"')";//string sqlstr = "insert into dbo.youweishuju(名称)values('" + title + "')";SqlCommand myCmd = new SqlCommand(sqlstr, myconn);myCmd.ExecuteNonQuery();myconn.Close();}}

  二,linq的方式

1、读取excel数据并且验证

 protected void UpLoad(){this.GvReport.DataSource = null;this.GvReport.DataBind();var fileName = string.Empty;try{if (this.FileUpload1.PostedFile != null && !string.IsNullOrEmpty(this.FileUpload1.PostedFile.FileName)){if (!".xls,.xlsx".Contains(Path.GetExtension(this.FileUpload1.PostedFile.FileName))){Response.Write("<script>alert('上传的文件不是EXCEL文件!请重新上传!');</script>");}else{var connectionString = string.Empty;fileName = Path.GetTempFileName();this.FileUpload1.PostedFile.SaveAs(fileName);connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileName + ";" + "Extended properties='Excel 12.0;HDR=yes;IMEX=1'";var sql = "Select * from [Sheet1$]";var dataAdapter = new OleDbDataAdapter(sql, connectionString);var ds = new DataSet();dataAdapter.Fill(ds, "ExcelInfo");var dataTable = ds.Tables["ExcelInfo"];var tb = new List<OrganizationBasis>();foreach (DataRow dr in dataTable.Rows){var query = new OrganizationBasis{StudentCode = dr["学号"].ToString().Trim(),StudentName = dr["姓名"].ToString().Trim(),SubjectName = dr["科目名称"].ToString().Trim(),//CourseName = dr["模块名称"].ToString().Trim(),//OrganizationNo = dr["班级编号"].ToString(),OrganizationName = dr["班级名称"].ToString(),SeatNo = dr["座位号"].ToString(),ClassTypeName = dr["班级类型"].ToString(),TeacherName = dr["任课教师"].ToString().Trim(),RoomName = dr["教室"].ToString(),WeekName = dr["星期"].ToString(),PeriodName = dr["节次"].ToString().Trim(),Remark = string.Empty,Status = true};tb.Add(query);}if (tb.Count() == decimal.Zero){ShowMessage("EXCLE内容为空,请核对并修改后再重新上传!");return;}using (var db = new SMSPModel.SMSPEntities()){var studentBasis = (from p in db.tbStudentBasiswhere p.RecordStatus == decimal.One && p.StudentStatus == decimal.Oneselect p).ToList();var teacherBasis = (from p in db.tbTeacherBasiswhere p.RecordStatus == decimal.Oneselect p).ToList();var gradBasis = (from p in db.tbGradeBasiswhere p.RecordStatus == decimal.Oneselect p).ToList();var classTypeBasis = (from p in db.tbClassTypewhere p.RecordStatus == decimal.Oneselect p).ToList();var orgStudent = (from p in db.tbOrganizationStudentwhere p.RecordStatus == decimal.One && p.tbStudentBasis.RecordStatus == decimal.One&& p.tbStudentBasis.StudentStatus == decimal.One && p.tbOrganizationBasis.RecordStatus == decimal.One&& p.tbOrganizationBasis.tbGradeBasis.ID == this.DrplstGrade1.GradeId&& p.tbOrganizationBasis.tbYear.ID == this.DrplstYear1.YearId&& p.tbOrganizationBasis.tbGradeBasis.ID == this.DrplstGrade1.GradeIdselect p).ToList();var orgTeacher = (from p in db.tbOrganizationTeacherwhere p.RecordStatus == decimal.One && p.tbTeacherBasis.RecordStatus == decimal.One&& p.tbOrganizationBasis.RecordStatus == decimal.One && p.tbOrganizationBasis.tbGradeBasis.ID == this.DrplstGrade1.GradeId&& p.tbOrganizationBasis.tbYear.ID == this.DrplstYear1.YearId&& p.tbOrganizationBasis.tbGradeBasis.ID == this.DrplstGrade1.GradeIdselect p).ToList();var orgMaster = (from p in db.tbOrganizationMasterwhere p.RecordStatus == decimal.One && p.tbTeacherBasis.RecordStatus == decimal.One&& p.tbOrganizationBasis.RecordStatus == decimal.One && p.tbOrganizationBasis.tbGradeBasis.ID == this.DrplstGrade1.GradeId&& p.tbOrganizationBasis.tbYear.ID == this.DrplstYear1.YearId&& p.tbOrganizationBasis.tbGradeBasis.ID == this.DrplstGrade1.GradeIdselect p).ToList();var orgBasis = (from p in db.tbOrganizationBasiswhere p.RecordStatus == decimal.One && p.tbGradeBasis.ID == this.DrplstGrade1.GradeId&& p.tbYear.ID == this.DrplstYear1.YearId && p.tbGradeBasis.ID == this.DrplstGrade1.GradeIdselect p).ToList();var jobBasis = (from p in db.tbTeacherJobwhere p.RecordStatus == decimal.Oneselect p).ToList();var courseBasis = (from p in db.tbCourseBasiswhere p.RecordStatus == decimal.Oneselect p).ToList();var YearId = (from i in db.tbYearwhere i.ID == (from p in db.tbYearwhere p.RecordStatus == decimal.One && p.ID == this.DrplstYear1.YearIdselect p.ParentID).FirstOrDefault()select i.ParentID).FirstOrDefault();var classBasis = (from p in db.tbClassBasiswhere p.RecordStatus == decimal.One && p.tbYear.ID == YearIdselect p).ToList();var roomBasis = (from p in db.tbBuildRoomwhere p.RecordStatus == decimal.Oneselect p).ToList();var weekBasis = (from p in db.tbWeekBasiswhere p.RecordStatus == decimal.Oneselect p).ToList();var periodBasis = (from p in db.tbPeriodBasiswhere p.RecordStatus == decimal.Oneselect p).ToList();foreach (var t in tb){if (!string.IsNullOrEmpty(t.StudentCode)){var tt = tb.Where(d => d.StudentCode == t.StudentCode && d.OrganizationName == t.OrganizationName && d.CourseName == t.CourseName).Count();if (tt > decimal.One){t.Remark = t.Remark + "该学号重复(" + tb.Where(d => d.StudentCode == t.StudentCode && d.OrganizationName == t.OrganizationName && d.CourseName == t.CourseName).Count() + "次);";}}if (!string.IsNullOrEmpty(t.StudentCode)){var studentCode = (from p in studentBasiswhere p.StudentCode == t.StudentCodeselect p).FirstOrDefault();if (studentCode == null){t.Remark = t.Remark + "该学号不存在;";}}if (!string.IsNullOrEmpty(t.StudentName)){var studentName = (from p in studentBasiswhere p.StudentCode == t.StudentCode && p.StudentName == t.StudentNameselect p).FirstOrDefault();if (studentName == null){t.Remark = t.Remark + "该学生学号与姓名不一致;";}}if (!string.IsNullOrEmpty(t.TeacherName)){var teacher = (from p in teacherBasiswhere p.TeacherName == t.TeacherNameselect p).FirstOrDefault();if (teacher == null){t.Remark = t.Remark + "该教师名称不存在;";}}if (!string.IsNullOrEmpty(t.ClassTypeName)){var classType = (from p in classTypeBasiswhere p.TypeName == t.ClassTypeNameselect p).FirstOrDefault();if (classType == null){t.Remark = t.Remark + "班级类型格式不正确;";}}if (!string.IsNullOrEmpty(t.RoomName)){var room = (from p in roomBasiswhere p.RoomName == t.RoomNameselect p).FirstOrDefault();if (room == null){t.Remark = t.Remark + "教室信息不存在;";}}if (!string.IsNullOrEmpty(t.SubjectName)){var subject = (from p in db.tbSubjectBasiswhere p.SubjectName == t.SubjectNameselect p).FirstOrDefault();if (subject == null){t.Remark += "科目信息不存在;";}}if (!string.IsNullOrEmpty(t.CourseName)){var course = (from p in db.tbCourseBasiswhere p.CourseName == t.CourseNameselect p).ToList();if (course.Count == decimal.Zero){t.Remark += "模块信息不存在;";}if (course.Where(c => c.tbSubjectBasis.SubjectName == t.SubjectName).Count() == decimal.Zero){t.Remark += "在【" + t.SubjectName + "】科目中没有该模块信息;";}}if (!string.IsNullOrEmpty(t.StudentCode) && string.IsNullOrEmpty(t.StudentName)){t.Remark = t.Remark + "姓名为必填字段;";}if (string.IsNullOrEmpty(t.StudentCode) && !string.IsNullOrEmpty(t.StudentName)){t.Remark = t.Remark + "学号为必填字段;";}}var t0 = tb.Where(c => c.Remark != string.Empty);if (t0.Count() == decimal.Zero){this.Save(db, studentBasis, teacherBasis, classTypeBasis, gradBasis, orgStudent, orgTeacher, orgMaster, orgBasis, classBasis, courseBasis, jobBasis, roomBasis, weekBasis, periodBasis, tb);}else{ShowMessage("EXCLE内容有错,请核对并修改后再重新上传!");this.GvReport.DataSource = t0;this.GvReport.DataBind();}}}}}catch (Exception e){if (e.Message.Equals("外部表不是预期的格式。")){var reader = XmlReader.Create(fileName);var dt = new DataTable();var row = 0;var isColumn = false;var col = 0;while (reader.Read()){if (reader.Name == "Row" && reader.NodeType == XmlNodeType.Element){row = row + 1;col = 0;if (row > 1){var dr = dt.NewRow();dt.Rows.Add(dr);}}if (reader.Name == "Cell" && reader.NodeType == XmlNodeType.Element){isColumn = true;if (reader.HasAttributes && reader.GetAttribute("ss:Index") != null){col = ConvertToInt(reader.GetAttribute("ss:Index"));}else{col = col + 1;}}if (reader.NodeType == XmlNodeType.Text){if (isColumn){if (row == 1){dt.Columns.Add(reader.Value);isColumn = false;}else{dt.Rows[dt.Rows.Count - 1][col - 1] = reader.Value;}}}if (reader.Name == "Row" && reader.NodeType == XmlNodeType.EndElement && row == 1){var dc = new DataColumn{DataType = Type.GetType("System.Boolean"),DefaultValue = true,ColumnName = "Status"};dt.Columns.Add(dc);dt.Columns.Add("Remark");}if (reader.Name == "Table" && reader.NodeType == XmlNodeType.EndElement){break;}}reader.Close();dt.AcceptChanges();for (var i = 0; i < dt.Columns.Count; i++){switch (dt.Columns[i].ColumnName){case "学号":dt.Columns[i].ColumnName = "StudentCode";break;case "姓名":dt.Columns[i].ColumnName = "StudentName";break;case "科目名称"://dt.Columns[i].ColumnName = "SubjectName";break;case "模块名称"://dt.Columns[i].ColumnName = "CourseName";break;case "班级编号":dt.Columns[i].ColumnName = "OrganizationNo";break;case "班级名称":dt.Columns[i].ColumnName = "OrganizationName";break;case "座位号":dt.Columns[i].ColumnName = "SeatNo";break;case "班级类型":dt.Columns[i].ColumnName = "ClassTypeName";break;case "任课教师":dt.Columns[i].ColumnName = "TeacherName";break;case "教室":dt.Columns[i].ColumnName = "RoomName";break;case "星期":dt.Columns[i].ColumnName = "WeekName";break;case "节次":dt.Columns[i].ColumnName = "PeriodName";break;default:break;}}var list = new List<OrganizationBasis>();var rows = dt.Select(string.Empty);using (var db = new SMSPModel.SMSPEntities()){var studentBasis = (from p in db.tbStudentBasiswhere p.RecordStatus == decimal.One && p.StudentStatus == decimal.Oneselect p).ToList();var teacherBasis = (from p in db.tbTeacherBasiswhere p.RecordStatus == decimal.Oneselect p).ToList();var gradBasis = (from p in db.tbGradeBasiswhere p.RecordStatus == decimal.Oneselect p).ToList();var classTypeBasis = (from p in db.tbClassTypewhere p.RecordStatus == decimal.Oneselect p).ToList();var orgStudent = (from p in db.tbOrganizationStudentwhere p.RecordStatus == decimal.One && p.tbStudentBasis.RecordStatus == decimal.One && p.tbStudentBasis.StudentStatus == decimal.One&& p.tbOrganizationBasis.RecordStatus == decimal.One && p.tbOrganizationBasis.tbGradeBasis.ID == this.DrplstGrade1.GradeId&& p.tbOrganizationBasis.tbYear.ID == this.DrplstYear1.YearIdselect p).ToList();var orgTeacher = (from p in db.tbOrganizationTeacherwhere p.RecordStatus == decimal.One && p.tbTeacherBasis.RecordStatus == decimal.One&& p.tbOrganizationBasis.RecordStatus == decimal.One && p.tbOrganizationBasis.tbGradeBasis.ID == this.DrplstGrade1.GradeId&& p.tbOrganizationBasis.tbYear.ID == this.DrplstYear1.YearIdselect p).ToList();var orgMaster = (from p in db.tbOrganizationMasterwhere p.RecordStatus == decimal.One && p.tbTeacherBasis.RecordStatus == decimal.One&& p.tbOrganizationBasis.RecordStatus == decimal.One && p.tbOrganizationBasis.tbGradeBasis.ID == this.DrplstGrade1.GradeId&& p.tbOrganizationBasis.tbYear.ID == this.DrplstYear1.YearIdselect p).ToList();var orgBasis = (from p in db.tbOrganizationBasiswhere p.RecordStatus == decimal.One && p.tbGradeBasis.ID == this.DrplstGrade1.GradeId&& p.tbYear.ID == this.DrplstYear1.YearIdselect p).ToList();var jobBasis = (from p in db.tbTeacherJobwhere p.RecordStatus == decimal.Oneselect p).ToList();var courseBasis = (from p in db.tbCourseBasiswhere p.RecordStatus == decimal.Oneselect p).ToList();var classBasis = (from p in db.tbClassBasiswhere p.RecordStatus == decimal.Oneselect p).ToList();var roomBasis = (from p in db.tbBuildRoomwhere p.RecordStatus == decimal.Oneselect p).ToList();var weekBasis = (from p in db.tbWeekBasiswhere p.RecordStatus == decimal.Oneselect p).ToList();var periodBasis = (from p in db.tbPeriodBasiswhere p.RecordStatus == decimal.Oneselect p).ToList();foreach (var dr in rows){var model = new OrganizationBasis{OrganizationName = dr["OrganizationName"].ToString(),OrganizationNo = dr["OrganizationNo"].ToString(),SeatNo = dr["SeatNo"].ToString(),StudentCode = dr["StudentCode"].ToString(),StudentName = dr["StudentName"].ToString(),ClassTypeName = dr["ClassTypeName"].ToString(),TeacherName = dr["TeacherName"].ToString(),Remark = dr["Remark"].ToString(),RoomName = dr["RoomName"].ToString(),Status = (bool)dr["Status"],SubjectName = dr["SubjectName"].ToString(),//CourseName = dr["CourseName"].ToString(),////PeriodName =dr["PeriodName"].ToString(),//WeekName=dr["WeekName"].ToString()};list.Add(model);if (!string.IsNullOrEmpty(model.StudentCode)){var studentCode = (from p in studentBasiswhere p.StudentCode == model.StudentCodeselect p).FirstOrDefault();if (studentCode == null){model.Remark = model.Remark + "该学号不存在;";}}if (!string.IsNullOrEmpty(model.StudentName)){var studentName = (from p in studentBasiswhere p.StudentCode == model.StudentCode && p.StudentName == model.StudentNameselect p).FirstOrDefault();if (studentName == null){model.Remark = model.Remark + "该学生学号与姓名不一致;";}}if (!string.IsNullOrEmpty(model.TeacherName)){var teacher = (from p in teacherBasiswhere p.TeacherName == model.TeacherNameselect p).FirstOrDefault();if (teacher == null){model.Remark = model.Remark + "该教师名称不存在;";}}if (!string.IsNullOrEmpty(model.ClassTypeName)){var classType = (from p in classTypeBasiswhere p.TypeName == model.ClassTypeNameselect p).FirstOrDefault();if (classType == null){model.Remark = model.Remark + "班级类型格式不正确;";}}if (!string.IsNullOrEmpty(model.RoomName)){var room = (from p in roomBasiswhere p.RoomName == model.RoomNameselect p).FirstOrDefault();if (room == null){model.Remark = model.Remark + "教室信息不存在;";}}if (!string.IsNullOrEmpty(model.SubjectName)){var subject = (from p in db.tbSubjectBasiswhere p.SubjectName == model.SubjectNameselect p).FirstOrDefault();if (subject == null){model.Remark += "科目信息不存在;";}}if (!string.IsNullOrEmpty(model.CourseName)){var course = (from p in db.tbCourseBasiswhere p.CourseName == model.CourseNameselect p).ToList();if (course.Count == decimal.Zero){model.Remark += "模块信息不存在;";}if (course.Where(c => c.tbSubjectBasis.SubjectName == model.SubjectName).Count() == decimal.Zero){model.Remark += "在【" + model.SubjectName + "】科目中没有该模块信息;";}}if (!string.IsNullOrEmpty(model.StudentCode) && string.IsNullOrEmpty(model.StudentName)){model.Remark = model.Remark + "姓名为必填字段;";}if (string.IsNullOrEmpty(model.StudentCode) && !string.IsNullOrEmpty(model.StudentName)){model.Remark = model.Remark + "学号为必填字段;";}}if (list.Count == decimal.Zero){ShowMessage("EXCLE内容为空,请核对并修改后再重新上传!");return;}foreach (var i in list){if (!string.IsNullOrEmpty(i.StudentCode)){var tt = list.Where(d => d.StudentCode == i.StudentCode && d.OrganizationName == i.OrganizationName && d.CourseName == i.CourseName).Count();if (tt > decimal.One){i.Remark = i.Remark + "该学号重复(" + list.Where(d => d.StudentCode == i.StudentCode && d.OrganizationName == i.OrganizationName && d.CourseName == i.CourseName).Count() + "次);";}}}var tb = list.Where(c => c.Remark != string.Empty);if (tb.Count() == decimal.Zero){this.Save(db, studentBasis, teacherBasis, classTypeBasis, gradBasis, orgStudent, orgTeacher, orgMaster, orgBasis, classBasis, courseBasis, jobBasis, roomBasis, weekBasis, periodBasis, list);}else{ShowMessage("EXCLE内容有错,请核对并修改后再重新上传!");this.GvReport.DataSource = tb;this.GvReport.DataBind();}}}else{ShowMessage("您上传Excel文件表头结构或表头名称与系统不匹配,请核查后再次操作!(参考信息:" + e.Message + ")");}}}

  2,将数据写到数据库

 protected void Save(SMSPModel.SMSPEntities db, List<SMSPModel.tbStudentBasis> studentBasis, List<SMSPModel.tbTeacherBasis> teacherBasis, List<SMSPModel.tbClassType> classTypeBasis, List<SMSPModel.tbGradeBasis> gradBasis, List<SMSPModel.tbOrganizationStudent> orgStudent, List<SMSPModel.tbOrganizationTeacher> orgTeacher, List<SMSPModel.tbOrganizationMaster> orgMaster, List<SMSPModel.tbOrganizationBasis> orgBasis, List<SMSPModel.tbClassBasis> classBasis, List<SMSPModel.tbCourseBasis> courseBasis, List<SMSPModel.tbTeacherJob> jobBasis, List<SMSPModel.tbBuildRoom> roomBasis, List<SMSPModel.tbWeekBasis> weekBasis, List<SMSPModel.tbPeriodBasis> periodBasis, List<OrganizationBasis> tb){var arrangeCourseBasis = (from p in db.tbArrangeCourseBasiswhere p.RecordStatus == decimal.One && p.ID == GuidDefaultselect p).FirstOrDefault();var year = (from p in db.tbYearwhere p.RecordStatus == decimal.One && p.ID == this.DrplstYear1.YearIdselect p).FirstOrDefault();var grad = (from p in gradBasiswhere p.ID == this.DrplstGrade1.GradeIdselect p).FirstOrDefault();var course = (from p in courseBasis//where p.ID == this.DrplstCourse1.CourseId ----KEN's Markselect p).FirstOrDefault();var oldOrgStudent = (from p in orgStudentselect p).ToList();var myOrg = (from p in tb.Where(d => !string.IsNullOrEmpty(d.OrganizationName))select new{p.OrganizationName,p.OrganizationNo,p.ClassTypeName,p.RoomName,p.CourseName,//p.SubjectName//}).Distinct().ToList();var Defaultclass = db.tbClassBasis.Where(d => d.RecordStatus == decimal.One && d.ID == GuidDefault).FirstOrDefault();foreach (var c in myOrg){var org = new SMSPModel.tbOrganizationBasis{ID = Guid.NewGuid(),OrganizationNo = ConvertToDecimal(c.OrganizationNo),OrganizationName = c.OrganizationName,tbYear = year,tbGradeBasis = grad,tbCourseBasis = courseBasis.Where(o => o.CourseName == c.CourseName).FirstOrDefault(), //course,tbBuildRoom = roomBasis.Where(d => d.RoomName == c.RoomName).FirstOrDefault() ?? roomBasis.Where(d => d.ID == GuidDefault).FirstOrDefault(),tbClassBasis = Defaultclass,tbTeacherJob = jobBasis.Where(d => d.ID == GuidDefault).FirstOrDefault(),tbClassType = classTypeBasis.Where(d => d.TypeName == c.ClassTypeName).FirstOrDefault(),tbArrangeCourseBasis = arrangeCourseBasis,RecordStatus = decimal.One,UpdateTime = DateTime.Now};db.AddTotbOrganizationBasis(org);}db.SaveChanges();var newOrg = (from p in db.tbOrganizationBasiswhere p.RecordStatus == decimal.One&& p.tbYear.ID == this.DrplstYear1.YearIdselect p).ToList();foreach (var t in tb){if (!string.IsNullOrEmpty(t.StudentCode) && !string.IsNullOrEmpty(t.StudentName)){var stu = new SMSPModel.tbOrganizationStudent{ID = Guid.NewGuid(),tbOrganizationBasis = newOrg.Where(d => d.OrganizationName == t.OrganizationName && d.tbCourseBasis.CourseName == t.CourseName && d.tbCourseBasis.tbSubjectBasis.SubjectName == t.SubjectName).FirstOrDefault(),SeatNo = t.SeatNo == null ? decimal.Zero : ConvertToDecimal(t.SeatNo),tbStudentBasis = studentBasis.Where(d => d.StudentCode == t.StudentCode).FirstOrDefault(),RecordStatus = decimal.One,UpdateTime = DateTime.Now};db.AddTotbOrganizationStudent(stu);}}var myTeacher = (from p in tb.Where(d => !string.IsNullOrEmpty(d.TeacherName))select new{p.TeacherName,p.OrganizationName}).Distinct().ToList();foreach (var mt in myTeacher){var tea = new SMSPModel.tbOrganizationTeacher{ID = Guid.NewGuid(),tbOrganizationBasis = newOrg.Where(d => d.OrganizationName == mt.OrganizationName).FirstOrDefault(),tbTeacherBasis = teacherBasis.Where(d => d.TeacherName == mt.TeacherName).FirstOrDefault(),RecordStatus = decimal.One,UpdateTime = DateTime.Now};db.AddTotbOrganizationTeacher(tea);}var myArrangeCourse = (from p in tb.Where(d => !string.IsNullOrEmpty(d.WeekName) && !string.IsNullOrEmpty(d.PeriodName))select new{p.OrganizationName,p.WeekName,p.PeriodName}).Distinct().ToList();foreach (var t in myArrangeCourse){var te = new SMSPModel.tbArrangeCourseResult{ID = Guid.NewGuid(),tbOrganizationBasis = newOrg.Where(d => d.OrganizationName == t.OrganizationName).FirstOrDefault(),tbWeekBasis = weekBasis.Where(d => d.WeekName == t.WeekName).FirstOrDefault(),tbPeriodBasis = periodBasis.Where(d => d.PeriodName == t.PeriodName).FirstOrDefault(),RecordStatus = decimal.One,UpdateTime = DateTime.Now};db.AddTotbArrangeCourseResult(te);}db.SaveChanges();ShowMessage("教学班导入成功!");}

  

 

 

  

转载于:https://www.cnblogs.com/kksguijiao/articles/4698512.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/492006.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

7大车企电动策略,平台化到底是解药还是毒药?

来源&#xff1a;车云电动化&#xff0c;已经成为当下汽车行业绕不开的话题&#xff0c;虽然几个国际主流汽车面对电动时代的态度都不一样&#xff08;各自的情况不同&#xff09;&#xff0c;但不得不说&#xff0c;它们都在积极筹备着这一产业转型带来的冲击。电动化平台&…

java中判空

一、概述 java中判等似乎很简单&#xff0c;用来判断对象引用&#xff08;内存地址&#xff09;是否相同&#xff0c;equals用来判断值是否相同。你可以试用String对象轻松区分这一点。 那么在null判等&#xff08;也就是判空操作&#xff09;时呢&#xff1f; 可以通过如下代码…

浙大神经中心李晓明教授团队在《自然-神经科学》发文发现恐惧情绪的新环路...

左起&#xff1a;余小丹&#xff0c;沈晨杰&#xff0c;付佳毓&#xff0c;王浩&#xff0c;李晓明&#xff0c;李春悦&#xff0c;郑迪来源 : 浙江大学神经科学研究所北京时间4月29日23时&#xff0c;浙江大学医学院李晓明教授课题组在《自然-神经科学》&#xff08;Nature Ne…

MTK+Android编译

1. 修改recovery代码 比如mediatek\custom\itek82_wet_kk\recovery\inc\cust_keys.h ./mk r k ./mk recoveryimage 更新recovery.img2.修改factory代码 factory mode related program and resources are built-in to system.img ./mk mm mediate/factory ./mk snod这会生成生成…

人工智能的社会、伦理与未来

来源&#xff1a;澎湃新闻人工智能的创新与社会应用方兴未艾&#xff0c;智能社会已见端倪。为了推动人工智能的社会、伦理与未来研究&#xff0c;构筑国内相关研究的网络与平台&#xff0c;中国社科院科学技术和社会研究中心、国家社科基金重大项目“智能革命与人类深度科技化…

Debug Assertion Failed _CrtlsValidHeapPointer(block) realloc堆引发的错误

应用场景 二级指针的应用中堆的处理 问题描述&#xff1a; realloc后出现堆错误 #include"algorithm.h"//数组插入元素 int insert(int **pp ,int &n,int tmp, int pos) {if (0 < pos&&pos < n){int *po *pp;//保存 旧一级指针int *pnNULL;//…

POJ 3928 amp; HDU 2492 Ping pong(树阵评价倒数)

主题链接&#xff1a; PKU:http://poj.org/problem?id3928 HDU:http://acm.hdu.edu.cn/showproblem.php?pid2492 Description N(3<N<20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill…

观点 | 李飞飞最新访谈:我每天都在对AI的担忧中醒来

来源&#xff1a;AI科技评论是的&#xff0c;最近这些年&#xff0c;无人机可以说是爆发式发展。以前&#xff0c;它仅用于神秘的军事用途。如今&#xff0c;却在我们的日常生活中见怪不怪。人工智能对人类会产生什么影响&#xff0c;一直是值得讨论的话题&#xff0c;它会更好…

数字图像处理 图像变换

数字图像基础参考资料一、人眼视觉特性二、图像基本知识1.图像类别2.图像输入3.图像输出三、图像处理1.图像类型转化2.图像变换点变换代数运算几何运算参考资料 一、人眼视觉特性 韦伯率:50%概率感觉到亮度变化时的 ΔII\frac{\Delta I}{I}IΔI​为韦伯率。 韦伯-费希纳定律&a…

超乎想象!关于5G无人机的最强科普!

来源&#xff1a;鲜枣课堂/xzclasscom说到无人机&#xff0c;大家一定都非常熟悉。是的&#xff0c;最近这些年&#xff0c;无人机可以说是爆发式发展。以前&#xff0c;它仅用于神秘的军事用途。如今&#xff0c;却在我们的日常生活中见怪不怪。我们身边出现了越来越多的无人机…

linux处置服务Iptables

一&#xff1a;Iptables防火墙服务 iptables分为两个部分&#xff1a;一个部分在内核中实现&#xff0c;一个为用户接口命令iptables,用户通过该命令来改动防火墙的功能。所以&#xff0c;iptables要使用对应的功能。必需要在内核中加入对应的模块。modprobe加入模块 1&#xf…

相关与卷积、各种误差

相关和卷积相关自相关性质卷积公式物理意义性质相关 自相关 自相关函数就是信号x(t)x(t)x(t)和它的时移信号 x(tτ)x(t\tau )x(tτ) 的乘积平均值。它是时移变量 τ\tauτ 的函数。 “自相关”这种数据处理方法&#xff0c;可以发现隐藏在杂乱信号中的有用信息。这个能力是…

报表reprot

if (contextMenuStrip1.Items[i].Text.Trim() "打印"){//1.在报表上绘制数据列&#xff0c;print.列名称&#xff08;如果是直接在数据源中连接数据库去拖数据列&#xff0c;不能共用&#xff09;//2注册的dt,print 表名.调用找到时根据表名//数据区域的类型DataBan…

5G全产业链最新解读

来源&#xff1a;中创产业研究院摘要&#xff1a;自5G概念的提出&#xff0c;各国相关技术的研发以及产业布局也在如火如荼进行之中。与此同时我国5G在标准研发上正逐渐成为全球领跑者&#xff0c;有望在2019年实现5G技术的试商用&#xff0c;在2020年实现正式商用。本文将围绕…

函数参数传递、数组指针、二级指针、左值、引用

数组和指针的区别 数组内地址一定连续&#xff0c;指针则不一定连续。数组赋值要么初始化就设定&#xff0c;要么循环元素赋值。而指针随时可以赋值(改变指向)。数组的存储空间&#xff0c;不是在静态区就是在栈上。指针位置随意。sizeof&#xff08;数组名&#xff09; 元素个…

下一个汽车万亿市场打开!氢能源时代正到来

来源&#xff1a;智东西内参看点&#xff1a;中国氢能汽车产业产值有望突破万亿元大关。氢能汽车是以氢为主要能量作为移动的汽车。氢能被视为全球最具发展潜力的清洁能源之一&#xff0c;并被不少国家、车企及学者认为是“终极新能源汽车解决方案”。伴随着汽车保有量持续增长…

高斯噪声和椒盐噪声

clear all;clc;close all; imimread(1.jpg); % oim; [w, h ,z]size(im); imrgb2gray(im); oim;%原图备份 figure;seq0; seqseq1;subplot(2,2,seq);imshow(im);title(原图);posaxis;xlabel(,position,[(pos(1)pos(2))/2 pos(4)]);%高斯噪声 gaunormrnd(0,0.1,w,h); im1im2double…

去苹果浏览器默认样式

去苹果浏览器默认样式 input,textarea,button { -webkit-appearance: none; border-radius:0px; border:none;}转载于:https://www.cnblogs.com/mcdnf/p/5158248.html

李彦宏/王坚/王传福等上榜“中国工程院2019院士增选候选人”引争议,实至名归吗?...

来源&#xff1a;物联网智库导 读五一小长假前一天&#xff0c;中国工程院网站公布了2019年工程院院士增选有效候选人名单。经中国工程院主席团审定&#xff0c;最终确定的有效候选人共531位。值得注意的是&#xff0c;BAT微软比亚迪等高管均上榜。此次中国工程院531名候选人的…

矩阵转置函数——指针自增的陷阱

#include"algorithm.h"int** transpose(int**po, int &m, int &n) {int*p (int*)malloc(m*n*sizeof(int));//转置矩阵空间int**pn (int**)malloc(n*sizeof(int*));//转置矩阵数组指针//指针备份int *save po[0];int *nSave p;//布置转置矩阵// [1 2 3 4 …