protected void addBtn_Click(object sender, EventArgs e) { string classTypeStr = Tools.safeUserInput(classTypeTB.Text.Trim().ToString()); if (classTypeStr.Length < 1) { MessageBox.Show(this, "课程类别不能为空!"); return; } classtype ClassType = new classtype(); ClassType.name = classTypeStr; classtypeBLL ClassTypeBLL = new classtypeBLL(); try { ClassTypeBLL.Add(ClassType); } catch { MessageBox.Show(this, "添加失败!"); return; } finally { } MessageBox.Show(this, "添加成功!"); GridView1.DataBind(); }
/// <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 { } }