/// <summary> /// 获取数据并展示 /// </summary> /// <param name="id"></param> public void GetData(int id) { DALstudent_info dal = new DALstudent_info(); student_infoEntity stu = dal.Getstudent_info(id); ViewState["stu"] = stu; //在当前页缓存数据。 lblStuID.Text = stu.StudentId; txtStuName.Text = stu.StudentName; //加载显示数据 Image1.ImageUrl = "img/studentphone/" + stu.StudentPhoto; txtStuNation.Text = stu.StudentNation; if (stu.StudentSex == "男") { rdlSexMale.Checked = true; } if (stu.StudentSex == "女") { rdlSexFemale.Checked = true; } txtStuTelehpone.Text = stu.StudentTelehpone; txtStuQQ.Text = stu.StudentQQ; txtStuClass.Text = stu.StudentClass; txtStuDormitory.Text = stu.StudentDormitory; txtStuAddress.Text = stu.StudentAddress; //将取回的数据在页面进行展示,可以修改的数据使用TextBox来展示,并提供修改。 //通过按钮触发,实现将数据编辑保存的功能 }
/// <summary> /// Update an item /// </summary> /// <param name="student_info"></param> /// <returns>count of rows affected</returns> public int Modstudent_info(student_infoEntity student_info) { int backvalue = 0; //get paras SqlParameter[] student_infoParms = Getstudent_infoParameters(); //set values to paras student_infoParms[0].Value = student_info.Id; student_infoParms[1].Value = student_info.StudentId; student_infoParms[2].Value = student_info.StudentPassword; student_infoParms[3].Value = student_info.StudentName; student_infoParms[4].Value = student_info.StudentPhoto; student_infoParms[5].Value = student_info.StudentSex; student_infoParms[6].Value = student_info.StudentNation; student_infoParms[7].Value = student_info.StudentTelehpone; student_infoParms[8].Value = student_info.StudentQQ; student_infoParms[9].Value = student_info.StudentClass; student_infoParms[10].Value = student_info.StudentDormitory; student_infoParms[11].Value = student_info.StudentAddress; //update using (SqlConnection conn = new SqlConnection(SQLHelper.ConnectionString)) { backvalue = SQLHelper.ExecuteNonQuery(SQLHelper.ConnectionString, CommandType.Text, UPDATE_SQL, student_infoParms); } return(backvalue); }
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int rowindex = int.Parse(e.CommandArgument.ToString()); //获取操作行的行号 string Id = GridView1.DataKeys[rowindex].Value.ToString(); //获取操作行数据的主键Id DALstudent_info dal = new DALstudent_info(); //定义针对admin_user表的操作对象 switch (e.CommandName) //获取操作对象的命令 { case "edit": //调转到编辑页面 Response.Redirect("StudentEdit.aspx?id=" + Id); break; case "del": //删除用户 dal.Delstudent_info(int.Parse(Id)); LoadData(); //重新加载数据,验证是否成功删除 this.Page.RegisterStartupScript("", "<script>alert('该学生信息已删除!');</script>"); break; case "reset": //修改密码 student_infoEntity admin = dal.Getstudent_info(int.Parse(Id)); admin.StudentPassword = "******"; dal.Modstudent_info(admin); LoadData(); //重新加载数据,验证是否重置 this.Page.RegisterStartupScript("", "<script>alert('该学生的密码重置成功!');</script>"); break; } }
protected void grdusers_RowCommand(object sender, GridViewCommandEventArgs e) { int rowindex = int.Parse(e.CommandArgument.ToString()); //获取操作行的行号 string Id = grdusers.DataKeys[rowindex].Value.ToString(); //获取操作行数据的主键Id DALstudent_info dal = new DALstudent_info(); //定义针对admin_user表的操作对象 switch (e.CommandName) //获取操作对象的命令 { case "edit": //调转到编辑页面 Response.Redirect("?id=" + Id); break; case "del": //删除用户 dal.Delstudent_info(int.Parse(Id)); LoadData(); //重新加载数据,验证是否成功删除 Response.Write("<script>alert('数据删除成功!')</script>"); break; case "reset": //修改密码 student_infoEntity u = dal.Getstudent_info(int.Parse(Id)); u.StudentPassword = "******"; dal.Modstudent_info(u); LoadData(); //重新加载数据,验证是否重置 Response.Write("<script>alert('用户密码重置成功,新密码123456')</script>"); break; } }
public void GetData(int id) { DALstudent_info dal = new DALstudent_info(); student_infoEntity student = dal.Getstudent_info(id); ViewState["student"] = student; StudentId.Value = student.StudentId; }
protected void Page_Load(object sender, EventArgs e) { DALstudent_info dal = new DALstudent_info(); student_infoEntity stu = dal.Getstudent_info(int.Parse(Session["id"].ToString())); lblName.Text = Session["studentName"].ToString(); Image1.ImageUrl = "./img/studentphone/" + stu.StudentPhoto; }
public void GetData(int id) { DALstudent_info dal = new DALstudent_info(); student_infoEntity student = dal.Getstudent_info(id); ViewState["student"] = student; select.Value = student.StudentId; //将取回的数据在页面进行展示,可以修改的数据使用TextBox来展示,并提供修改。 //通过按钮触发,实现将数据编辑保存的功能 }
/// <summary> /// 保存修改 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnedit_Click(object sender, EventArgs e) { DALstudent_info dal = new DALstudent_info(); student_infoEntity student = (student_infoEntity)ViewState["student"];//获取缓存 if (student.StudentPassword == Password1.Value) { student.StudentPassword = L_pass.Value; //修改数据 dal.Modstudent_info(student); //提交到数据库 ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('修改成功!');</script>"); } else { ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('修改失败,请检查密码正确性!');</script>"); } }
protected void btnAdd_Click(object sender, EventArgs e) { student_infoEntity stu = new student_infoEntity();//实体对象 stu.StudentId = txtStuID.Text.Trim(); stu.StudentPassword = "******"; stu.StudentName = txtStuName.Text; stu.StudentPhoto = imgUpLoad(fulStuPhoto); if (rdlSexMale.Checked) { stu.StudentSex = "男"; } if (rdlSexFemale.Checked) { stu.StudentSex = "女"; } stu.StudentNation = txtStuNation.Text; stu.StudentTelehpone = txtStuTelehpone.Text; stu.StudentQQ = txtStuQQ.Text; stu.StudentClass = txtStuClass.Text; stu.StudentDormitory = txtStuDormitory.Text; stu.StudentAddress = txtStuAddress.Text; if (dal.Addstudent_info(stu) > 0) { this.Page.RegisterStartupScript("", "<script>alert('学生信息添加成功!')</script>"); Response.Redirect("StudentFind.aspx"); } else { this.Page.RegisterStartupScript("", "<script>alert('学生信息添加失败!');</script>"); } //if (dal.Addadmin_user(admin) > 0)//执行 //{ // this.Page.RegisterStartupScript("", "<script>alert('用户新增成功!')</script>"); // Response.Redirect("AdminList.aspx"); //} //else // this.Page.RegisterStartupScript("", "<script>alert('用户新增失败!');</script>"); }
/// <summary> /// Select items by Columns /// </summary> /// <returns>IList<student_infoEntity></returns> public IList <student_infoEntity> Getstudent_infosTopN(int n) { IList <student_infoEntity> student_infos = new List <student_infoEntity>(); using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SELECT_ALL_SQL, null)) { int i = 0; while (reader.Read() && i < n) { student_infoEntity student_info = new student_infoEntity(); if (!reader.IsDBNull(0)) { student_info.Id = reader.GetInt32(0); } if (!reader.IsDBNull(1)) { student_info.StudentId = reader.GetString(1); } if (!reader.IsDBNull(2)) { student_info.StudentPassword = reader.GetString(2); } if (!reader.IsDBNull(3)) { student_info.StudentName = reader.GetString(3); } if (!reader.IsDBNull(4)) { student_info.StudentPhoto = reader.GetString(4); } if (!reader.IsDBNull(5)) { student_info.StudentSex = reader.GetString(5); } if (!reader.IsDBNull(6)) { student_info.StudentNation = reader.GetString(6); } if (!reader.IsDBNull(7)) { student_info.StudentTelehpone = reader.GetString(7); } if (!reader.IsDBNull(8)) { student_info.StudentQQ = reader.GetString(8); } if (!reader.IsDBNull(9)) { student_info.StudentClass = reader.GetString(9); } if (!reader.IsDBNull(10)) { student_info.StudentDormitory = reader.GetString(10); } if (!reader.IsDBNull(11)) { student_info.StudentAddress = reader.GetString(11); } student_infos.Add(student_info); i++; } } return(student_infos); }
/// <summary> /// Select an item by Primarykey /// </summary> /// <param name="id"></param> /// <returns>an item</returns> public student_infoEntity Getstudent_info(int id) { student_infoEntity student_info = new student_infoEntity(); //primarykeys //get paras SqlParameter[] student_infoParms = Getstudent_infoPrimaryKeyParameters(); //set values to paras student_infoParms[0].Value = id; //select using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.ConnectionString, CommandType.Text, SELECT_ONE_SQL, student_infoParms)) { if (reader.Read()) { if (!reader.IsDBNull(0)) { student_info.Id = reader.GetInt32(0); } if (!reader.IsDBNull(1)) { student_info.StudentId = reader.GetString(1); } if (!reader.IsDBNull(2)) { student_info.StudentPassword = reader.GetString(2); } if (!reader.IsDBNull(3)) { student_info.StudentName = reader.GetString(3); } if (!reader.IsDBNull(4)) { student_info.StudentPhoto = reader.GetString(4); } if (!reader.IsDBNull(5)) { student_info.StudentSex = reader.GetString(5); } if (!reader.IsDBNull(6)) { student_info.StudentNation = reader.GetString(6); } if (!reader.IsDBNull(7)) { student_info.StudentTelehpone = reader.GetString(7); } if (!reader.IsDBNull(8)) { student_info.StudentQQ = reader.GetString(8); } if (!reader.IsDBNull(9)) { student_info.StudentClass = reader.GetString(9); } if (!reader.IsDBNull(10)) { student_info.StudentDormitory = reader.GetString(10); } if (!reader.IsDBNull(11)) { student_info.StudentAddress = reader.GetString(11); } } else { student_info = null; } } return(student_info); }
/// <summary> /// 导入Excel文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnImport_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { int startPosition = FileUpload1.FileName.LastIndexOf("."); //获取后缀名的起始位置 string extName = FileUpload1.FileName.Substring(startPosition).ToLower(); //获取后缀名 if (startPosition >= 0) { if (extName.Equals(".xlsx") || extName.Equals(".xls")) { string path = Server.MapPath("./File/"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string saveFile = path + DateTime.Now.ToString("yyyyMMddHHmmss") + extName; FileUpload1.SaveAs(saveFile);//上传文件到指定位置 string str = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + saveFile + ";Extended Properties='Excel 12.0;HDR=Yes;IMEX=1;'"; OleDbConnection oledbconn = new OleDbConnection(str); //新建ole命令对象 oledbconn.Open(); ///因为需要在运行的过程中,增加数据列,并对数据进行修正。 ///所以,需要首先读入数据进入内存datatable中然后进行操作。 OleDbCommand commandSourceData = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbconn); OleDbDataAdapter oleda = new OleDbDataAdapter(); oleda.SelectCommand = commandSourceData; DataTable tb = new DataTable(); oleda.Fill(tb); oledbconn.Close(); //数据规整部分。 int successNumber = 0; int failureNumber = 0; //数据处理 StringBuilder resultsb = new StringBuilder(); DALstudent_info dal = new DALstudent_info(); foreach (DataRow dr in tb.Rows) { try { //取值 string sNo = dr["sNo"].ToString().Trim(); string pwd = dr["pwd"].ToString().Trim(); string name = dr["name"].ToString().Trim(); string photo = dr["photo"].ToString().Trim(); string sex = dr["sex"].ToString().Trim(); string nation = dr["nation"].ToString().Trim(); string tel = dr["tel"].ToString().Trim(); string sQQ = dr["sQQ"].ToString().Trim(); string sClass = dr["sClass"].ToString().Trim(); string dormitory = dr["dormitory"].ToString().Trim(); string address = dr["address"].ToString().Trim(); student_infoEntity student = new student_infoEntity(); //赋值 student.StudentId = sNo; student.StudentPassword = pwd; student.StudentName = name; student.StudentPhoto = photo; student.StudentSex = sex; student.StudentNation = nation; student.StudentTelehpone = tel; student.StudentQQ = sQQ; student.StudentClass = sClass; student.StudentDormitory = dormitory; student.StudentAddress = address; dal.Addstudent_info(student);//添加 successNumber++; } catch (Exception Error) { resultsb.Append(dr["sNo"].ToString().Trim()); resultsb.Append("导入失败;原因:"); resultsb.Append(Error.Message); resultsb.Append("\r\n"); failureNumber++; } }//endforeach //this.Page.RegisterStartupScript("", "<script>alert('" + successNumber + " 条信息成功导入!\r\n" + failureNumber + " 条信息无法导入!\r\n');</script>"); Response.Redirect("StudentFind.aspx"); //ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('" + successNumber + " 条信息成功导入!\r\n" + failureNumber + " 条信息无法导入!\r\n');</script>"); } else { ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('选定的文件不是合法的Excel文件!');</script>"); } } } else { ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('没有选定的要导入的文件!');</script>"); } }