protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnCX_Click(object sender, EventArgs e)
{
GridView1.DataSource = new tbStuInfoManager().GetStuBySname(TextBox2.Text);
GridView1.DataBind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sno = (GridView1.Rows[e.RowIndex].FindControl("LinkButton1") as LinkButton).CommandArgument;
if (new tbStuInfoManager().DeleteStuInfo(sno))
{
Response.Write("<script>alert('删除成功')</script>");
GridView1.DataSource = new tbStuInfoManager().GetStuBySname(TextBox2.Text);
GridView1.DataBind();
}
else
Response.Write("<script>alert('删除失败')</script>");
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataSource = new tbStuInfoManager().GetStuBySname(TextBox2.Text);
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
tbStuInfo tbstu=new tbStuInfo ();
tbstu.Sno = (GridView1.Rows[e.RowIndex].FindControl("LinkButton1") as LinkButton).CommandArgument;
tbstu.Sname = (GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox).Text;
tbstu.Sex = (GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox).Text;
tbstu.ZhuanYe = (GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text;
tbstu.JiGuan = (GridView1.Rows[e.RowIndex].FindControl("TextBox4") as TextBox).Text;
if (new tbStuInfoManager().ModifyStuInfo(tbstu))
{
Response.Write("<script>alert('修改成功')</script>");
GridView1.EditIndex = -1;//退出编辑状态
GridView1.DataSource = new tbStuInfoManager().GetStuBySname(TextBox2.Text);
GridView1.DataBind();
}
else
Response.Write("<script>alert('修改失败')</script>");
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;//退出编辑状态
GridView1.DataSource = new tbStuInfoManager().GetStuBySname(TextBox2.Text);
GridView1.DataBind();
}
public bool AddStuInfo(tbStuInfo tbstu)
{
string sql = string.Format("insert into tbStuInfos(Sno,Sname,Sex,ZhuanYe,JiGuan)values('{0}','{1}','{2}','{3}','{4}')", tbstu.Sno, tbstu.Sname, tbstu.Sex, tbstu.ZhuanYe, tbstu.JiGuan);
return DBHelper.ExecuteNonQuery(DBHelper.ConnectionString, CommandType.Text, sql)>0;
}
public List<tbStuInfo> GetStuBySname(string name)
{
string sql = string.Format("select * from tbStuInfos where Sname like '%{0}%'", name);
SqlDataReader dr = DBHelper.ExecuteReader(DBHelper.ConnectionString, CommandType.Text, sql);
List<tbStuInfo> list=new List<tbStuInfo> ();
while (dr.Read())
{
tbStuInfo tbf = new tbStuInfo();
tbf.Sno = Convert.ToString(dr["Sno"]);
tbf.Sname = Convert.ToString(dr["Sname"]);
tbf.Sex = Convert.ToString(dr["Sex"]);
tbf.ZhuanYe = Convert.ToString(dr["ZhuanYe"]);
tbf.JiGuan = Convert.ToString(dr["JiGuan"]);
list.Add(tbf);
}
dr.Close();
return list;
}
public bool DeleteStuInfo(string sno)
{
string sql = string.Format("delete tbStuInfos where Sno='{0}'", sno);
return DBHelper.ExecuteNonQuery(DBHelper.ConnectionString, CommandType.Text, sql) > 0;
}
public bool ModifyStuInfo(tbStuInfo tbs)
{
string sql = string.Format("update tbStuInfos set Sname='{0}',Sex='{1}',ZhuanYe='{2}',JiGuan='{3}' where Sno='{4}'", tbs.Sname, tbs.Sex, tbs.ZhuanYe, tbs.JiGuan, tbs.Sno);
return DBHelper.ExecuteNonQuery(DBHelper.ConnectionString, CommandType.Text, sql) > 0;
}
}