protected void addBtn_Click(object sender, EventArgs e)
 {
     examrecordBLL ExamRecordBLL = new examrecordBLL();
     examrecord ExamRecord = new examrecord();
     string stuIdStr = Tools.safeUserInput(stuIdTB.Text.ToString().Trim());
     if (stuIdStr.Length < 1)
     {
         MessageBox.Show(this, "学生学号不能为空!");
         return;
     }
     else
     {
         ExamRecord.student = stuIdStr;
     }
     string scoreStr = Tools.safeUserInput(scoreTB.Text.ToString().Trim());
     if (scoreStr.Length < 1)
     {
         MessageBox.Show(this, "学生成绩不能为空!");
         return;
     }
     else
     {
         int scoreInt = -1;
         try
         {
             scoreInt = Convert.ToInt32(scoreStr);
         }
         catch
         {
             MessageBox.Show(this, "学生成绩只能为数字!");
             return;
         }
         if (scoreInt != -1)
             ExamRecord.score = scoreInt.ToString();
         else MessageBox.Show(this, "学生成绩只能为数字!");
     }
     string remarkStr = Tools.safeUserInput(remarkTB.Text.ToString().Trim());
     ExamRecord.remark = remarkStr;
     ExamRecord.classId = id.ToString();
     if (ExamRecordBLL.Add(ExamRecord) != 0)
     {
         MessageBox.Show(this, "添加成功!");
         GV2DataBind();
     }
     else MessageBox.Show(this, "添加失败!");
 }
 protected void Button3_Click(object sender, EventArgs e)
 {
     int classId = 0;
     int score = 0;
     string remark = "";
     try
     {
         classId = Convert.ToInt32(TextBox1.Text.Trim());
         score = Convert.ToInt32(TextBox2.Text.Trim());
         remark = Tools.safeUserInput(TextBox3.Text.Trim());
     }
     catch
     {
         MessageBox.Show(this, "输入格式不对,请重新输入!");
     }
     classdetailBLL ClassDetailBLL = new classdetailBLL();
     if (!ClassDetailBLL.Exists(classId))
     {
         MessageBox.Show(this, "课程不存在!");
         return;
     }
     examrecord ExamRecord = new examrecord();
     examrecordBLL ExamRecordBLL = new examrecordBLL();
     ExamRecord.classId = classId.ToString();
     ExamRecord.score = score.ToString();
     ExamRecord.student = Session["StuId"].ToString().Trim();
     ExamRecord.remark = remark;
     try
     {
         ExamRecordBLL.Add(ExamRecord);
     }
     catch
     {
         MessageBox.Show(this, "添加成绩失败!");
     }
     MessageBox.Show(this, "添加" + ClassDetailBLL.GetModel(classId).teacher.Trim() + "的" + ClassDetailBLL.GetModel(classId).name + ",分数:"+score+"。");
     GridView1.DataBind();
 }
        /// <summary>
        /// 添加EXCEL信息
        /// </summary>
        public int Add(Page page, FileUpload fu, int classId)
        {
            examrecordDAL ExamRecordDAL = new examrecordDAL();
            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/scoreInput/");
                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++)
                    {
                        if (isInClass(dr[i]["任课教师"].ToString(), dr[i]["课程名称"].ToString(), classId))
                        {
                            examrecord ExamRecord = new examrecord();
                            string stuId = dr[i]["学生学号"].ToString();
                            if (stuId.Length < 1)
                            {
                                MessageBox.Show(page, "导入第" + (i + 1).ToString() + "个成绩信息失败,学生学号不能为空,请检查数据");
                                return i;
                            }
                            else
                            {
                                studentBLL StudentBLL = new studentBLL();
                                student Student = null;
                                try
                                {
                                    Student = StudentBLL.GetModelList("stuId='" + stuId + "'")[0];
                                }
                                catch
                                {
                                    MessageBox.Show(page, "导入第" + (i + 1).ToString() + "个成绩信息失败,学生学号不存在,请检查数据");
                                    return i;
                                }
                                if (Student != null)
                                {
                                    if (Student.name.Trim().Equals(dr[i]["学生姓名"].ToString().Trim()))
                                    {
                                        ExamRecord.student = stuId;
                                    }
                                    else
                                    {
                                        MessageBox.Show(page, "导入第" + (i + 1).ToString() + "个成绩信息失败,学生学号不存在,请检查数据");
                                        return i;
                                    }
                                }

                            }
                            int score = 0;
                            try
                            {
                                score = Convert.ToInt32(dr[i]["成绩"].ToString());
                            }
                            catch
                            {
                                MessageBox.Show(page, "导入第" + (i + 1).ToString() + "个成绩信息失败,学生成绩仅可以为数字且不能为空,请检查数据");
                                return i;
                            }
                            if (score != 0)
                            {
                                ExamRecord.score = score.ToString();
                            }
                            else
                            {
                                MessageBox.Show(page, "导入第" + (i + 1).ToString() + "个成绩信息失败,学生成绩仅可以为数字且不能为空,请检查数据");
                                return i;
                            }
                            ExamRecord.classId = classId.ToString();
                            ExamRecordDAL.Add(ExamRecord);
                        }
                        else
                        {
                            MessageBox.Show(page, "导入第" + (i + 1).ToString() + "个成绩信息失败,学生课程名称不存在,请检查数据");
                            return i;
                        }
                    }
                    return i;
                }

            }
            catch { return 0; }
            finally { }
        }
 /// <summary>
 /// 获得数据列表
 /// </summary>
 public List<GS.Model.examrecord> DataTableToList(DataTable dt)
 {
     List<GS.Model.examrecord> modelList = new List<GS.Model.examrecord>();
     int rowsCount = dt.Rows.Count;
     if (rowsCount > 0)
     {
         GS.Model.examrecord model;
         for (int n = 0; n < rowsCount; n++)
         {
             model = new GS.Model.examrecord();
             if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
             {
                 model.id = long.Parse(dt.Rows[n]["id"].ToString());
             }
             if (dt.Rows[n]["student"] != null && dt.Rows[n]["student"].ToString() != "")
             {
                 model.student = dt.Rows[n]["student"].ToString();
             }
             if (dt.Rows[n]["class"] != null && dt.Rows[n]["class"].ToString() != "")
             {
                 model.classId = dt.Rows[n]["class"].ToString();
             }
             if (dt.Rows[n]["score"] != null && dt.Rows[n]["score"].ToString() != "")
             {
                 model.score = dt.Rows[n]["score"].ToString();
             }
             modelList.Add(model);
         }
     }
     return modelList;
 }
 protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {
     examrecordBLL ExamRecordBLL = new examrecordBLL();
     examrecord ExamRecord = new examrecord();
     ExamRecord.id = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value);
     ExamRecord.student = ExamRecordBLL.GetModel(ExamRecord.id).student;
     try
     {
         ExamRecord.score = (Convert.ToInt32(((TextBox)(GridView2.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim())).ToString();
         ExamRecord.remark = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim();
     }
     catch
     {
         MessageBox.Show(this, "成绩只能为数字!更新失败!");
         return;
     }
     if (ExamRecordBLL.Update(ExamRecord)) MessageBox.Show(this, "更新成功!");
     else MessageBox.Show(this, "更新失败!");
     GridView2.EditIndex = -1;
     GV2DataBind();
 }