/// <summary> /// 获得数据列表 /// </summary> public List<GS.Model.classdetail> DataTableToList(DataTable dt) { List<GS.Model.classdetail> modelList = new List<GS.Model.classdetail>(); int rowsCount = dt.Rows.Count; if (rowsCount > 0) { GS.Model.classdetail model; for (int n = 0; n < rowsCount; n++) { model = new GS.Model.classdetail(); if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "") { model.id = int.Parse(dt.Rows[n]["id"].ToString()); } if (dt.Rows[n]["name"] != null && dt.Rows[n]["name"].ToString() != "") { model.name = dt.Rows[n]["name"].ToString(); } if (dt.Rows[n]["type"] != null && dt.Rows[n]["type"].ToString() != "") { model.type = Convert.ToInt32(dt.Rows[n]["type"].ToString()); } if (dt.Rows[n]["teacher"] != null && dt.Rows[n]["teacher"].ToString() != "") { model.teacher = dt.Rows[n]["teacher"].ToString(); } if (dt.Rows[n]["startTime"] != null && dt.Rows[n]["startTime"].ToString() != "") { model.startTime = DateTime.Parse(dt.Rows[n]["startTime"].ToString()); } if (dt.Rows[n]["endTime"] != null && dt.Rows[n]["endTime"].ToString() != "") { model.endTime = DateTime.Parse(dt.Rows[n]["endTime"].ToString()); } if (dt.Rows[n]["period"] != null && dt.Rows[n]["period"].ToString() != "") { model.period =dt.Rows[n]["period"].ToString(); } if (dt.Rows[n]["credit"] != null && dt.Rows[n]["credit"].ToString() != "") { model.credit = dt.Rows[n]["credit"].ToString(); } if (dt.Rows[n]["location"] != null && dt.Rows[n]["location"].ToString() != "") { model.location = dt.Rows[n]["location"].ToString(); } if (dt.Rows[n]["remark"] != null && dt.Rows[n]["remark"].ToString() != "") { model.remark = dt.Rows[n]["remark"].ToString(); } if (dt.Rows[n]["classId"] != null && dt.Rows[n]["classId"].ToString() != "") { model.classId = dt.Rows[n]["classId"].ToString(); } modelList.Add(model); } } return modelList; }
/// <summary> /// 增加excel数据库 /// </summary> public int Add(Page page, FileUpload fu) { classtypeBLL ClassTypeBLL=new classtypeBLL(); classdetailBLL ClassDetailBLL = new classdetailBLL(); try { int i = 0; if (fu.HasFile == false) { MessageBox.Show(page, "请选择您要上传的Excel文件!"); return 0;//当无文件时,返回 } string IsXls = System.IO.Path.GetExtension(fu.FileName).ToString().ToLower(); if (IsXls != ".xls") { MessageBox.Show(page, "只可以上传Excel文件!"); return 0;//当选择的不是Excel文件时,返回 } string path = page.Server.MapPath("storage/classInput/"); string strpath = fu.PostedFile.FileName.ToString(); //获取Execle文件路径 string filename = "批量课程信息" + System.DateTime.Now.ToString("yyyyMMddHHmmss").Trim() + ".xls"; //从时间获取文件路径 fu.PostedFile.SaveAs(path + filename); DataSet ds = Tools.ExecleDs(path + filename, filename); DataRow[] dr = ds.Tables[0].Select(); //定义一个DataRow数组 int rowsnum = ds.Tables[0].Rows.Count; if (rowsnum == 0) { //Response.Write("<script>alert('Excel表为空表,无数据!')</script>"); MessageBox.Show(page, "Excel表为空表,无数据!");//当Excel表为空时,对用户进行提示 return 0; } else { for (i = 0; i < dr.Length; i++) { classdetail ClassDetail = new classdetail(); string name = dr[i]["课程名称"].ToString(); if (name.Length < 1) { MessageBox.Show(page, "导入第" + (i + 1).ToString() + "门课程信息失败,课程名称不能为空,请检查数据"); return i; } else ClassDetail.name = name; int classType=0; try { classType = Convert.ToInt32(dr[i]["课程类别"]); } catch { MessageBox.Show(page, "导入第" + (i + 1).ToString() + "门课程信息失败,课程类别必须为数字,请检查数据"); return i; } if (!(ClassTypeBLL.Exists(classType.ToString()))) { MessageBox.Show(page, "导入第" + (i + 1).ToString() + "门课程信息失败,课程类别不存在,请检查数据"); return i; } else ClassDetail.type = classType; ClassDetail.teacher = dr[i]["教师姓名"].ToString(); try { ClassDetail.startTime = Convert.ToDateTime(dr[i]["开始时间"]); } catch { MessageBox.Show(page, "导入第" + (i + 1).ToString() + "门课程信息失败,开始时间格式不正确,请确保为“yyyy/mm/dd hh:mm:ss”,请检查数据"); return i; } try { ClassDetail.endTime = Convert.ToDateTime(dr[i]["结束时间"]); } catch { MessageBox.Show(page, "导入第" + (i + 1).ToString() + "门课程信息失败,结束时间格式不正确,请确保为“yyyy/mm/dd hh:mm:ss”,请检查数据"); return i; } ClassDetail.period = dr[i]["学时"].ToString(); ClassDetail.credit = dr[i]["学分"].ToString(); ClassDetail.location = dr[i]["开课地点"].ToString(); ClassDetail.remark = dr[i]["备注"].ToString(); ClassDetailBLL.Add(ClassDetail); } return i ; } } catch { return 0; } finally { } }
protected void addStudentBtn_Click(object sender, EventArgs e) { classdetail ClassDetail = new classdetail(); if (isTextBoxEmpty(nameTB, "课程名称")) ClassDetail.name = getText(nameTB); else return; ClassDetail.type = Convert.ToInt32(typeDDL.SelectedItem.Value); if (isTextBoxEmpty(teacherTB, "教师姓名")) ClassDetail.teacher= getText(teacherTB); else return; string startTimeStr = ""; if (isTextBoxEmpty(startYearTB, "开始时间-年")) { string year = getText(startYearTB); if (year.Length != 4) { MessageBox.Show(this, "开始时间-年:格式不正确!"); return; } startTimeStr += year+"-"; } if (isTextBoxEmpty(startMonthTB, "开始时间-月")) { int month = Convert.ToInt32(getText(startMonthTB)); if (month<1||month>12) { MessageBox.Show(this, "开始时间-月:格式不正确!"); return; } if (month < 10) startTimeStr += "0" + month.ToString()+"-"; else startTimeStr += month.ToString()+"-"; } if (isTextBoxEmpty(startDayTB, "开始时间-日")) { int day = Convert.ToInt32(getText(startDayTB)); if (day < 1 || day > 31) { MessageBox.Show(this, "开始时间-日:格式不正确!"); return; } if(day<10) startTimeStr += "0"+day.ToString(); else startTimeStr += day.ToString() ; } ClassDetail.startTime=Convert.ToDateTime(startTimeStr); string endTimeStr = ""; if (isTextBoxEmpty(endYearTB, "结束时间-年")) { string year = getText(endYearTB); if (year.Length != 4) { MessageBox.Show(this, "结束时间-年:格式不正确!"); return; } endTimeStr += year + "-"; } if (isTextBoxEmpty(endMonthTB, "结束时间-月")) { int month = Convert.ToInt32(getText(endMonthTB)); if (month<1||month>12) { MessageBox.Show(this, "结束时间-月:格式不正确!"); return; } if (month < 10) endTimeStr += "0" + month.ToString() + "-"; else endTimeStr += month.ToString() + "-"; } if (isTextBoxEmpty(endDayTB, "结束时间-日")) { int day = Convert.ToInt32(getText(endDayTB)); if (day < 1 || day > 31) { MessageBox.Show(this, "结束时间-日:格式不正确!"); return; } if (day < 10) endTimeStr += "0" + day.ToString(); else endTimeStr += day.ToString(); } ClassDetail.endTime=Convert.ToDateTime(endTimeStr); if (isTextBoxEmpty(periodTB, "学时")) ClassDetail.period = getText(periodTB); else return; if (isTextBoxEmpty(creditTB, "学分")) ClassDetail.credit = getText(creditTB); else return; if (isTextBoxEmpty(locationTB, "开课地点")) ClassDetail.location = getText(locationTB); else return; ClassDetail.remark=getText(remarkTB); try { classdetailBLL ClassDetailBLL = new classdetailBLL(); ClassDetailBLL.Add(ClassDetail); } catch(Exception ex) { MessageBox.Show(this, "添加失败!"); Console.WriteLine(ex.Message); } MessageBox.Show(this, "添加成功!"); }