3,881
社区成员
发帖
与我相关
我的任务
分享
//根据学生姓名查询学生信息
private void QryStuN_Click(object sender, System.EventArgs e)
{
try
{
UpdStustrconnName="select Student_id as 学号,Student_name as 姓名,Sex as 性别,Birth as 出生年月,Nation as 民族,Class_id as 班级,Entrance_date as 入学时间,home as 家庭地址,politic as 政治面貌,ID as 身份证号,Job as 职位,specialty as 所学专业 from student where student_name="+"'"+this.QryStuName.Text.ToString().Trim()+"'";
UpdSconn=new SqlConnection(UpdStuconnstr);
UpdStuGrdsqlda=new SqlDataAdapter(UpdStustrconnName,UpdSconn);
UpdNameStudset.Clear();
UpdStuGrdsqlda.Fill(UpdNameStudset,"student");
UpdStuGrdDg.DataSource=UpdNameStudset.Tables[0];
}
catch
{
MessageBox.Show("查询失败。","确认");
}
}
//根据学生班级查询学生信息
private void QueryStuClass_Click(object sender, System.EventArgs e)
{
try
{
UpdStustrconnClass="select Student_id as 学号,Student_name as 姓名,Sex as 性别,Birth as 出生年月,Nation as 民族,student.Class_id as 班级,Entrance_date as 入学时间,home as 家庭地址,politic as 政治面貌,ID as 身份证号,Job as 职位,specialty as 所学专业 from student,class where class.class_id=student.class_id and class.Class_name="+"'"+this.QryStuCla.SelectedItem.ToString().Trim()+"'";
UpdSconn=new SqlConnection(UpdStuconnstr);
UpdStuGrdsqlda=new SqlDataAdapter(UpdStustrconnClass,UpdSconn);
UpdClassStudset.Clear();
UpdStuGrdsqlda.Fill(UpdClassStudset,"student");
UpdStuGrdDg.DataSource=UpdClassStudset.Tables[0];
}
catch
{
MessageBox.Show("查询失败。","确认");
}
}
//保存修改
private void SaveChg_Click(object sender, System.EventArgs e)
{
try
{
int row=this.UpdStuGrdDg.CurrentCell.RowNumber;
this.UpdStuGrdDg.CurrentCell=new DataGridCell(row+1,0);
if(UpdClassStudset.HasChanges())//如果改动是在根据学生班级检索的结果集上进行的那么,更改该结果集
{
UpdSconn=new SqlConnection(UpdStuconnstr);
UpdStuGrdsqlda=new SqlDataAdapter(this.UpdStustrconnClass,this.UpdSconn);
SqlComm=new SqlCommandBuilder(UpdStuGrdsqlda);
this.UpdStuGrdsqlda.Update(this.UpdClassStudset.GetChanges(),"student");
MessageBox.Show("保存修改成功!","信息");
}
else if(UpdNameStudset.HasChanges())//如果改动是在根据学生姓名检索的结果集上进行的那么,更改该结果集
{
UpdSconn=new SqlConnection(UpdStuconnstr);
UpdStuGrdsqlda=new SqlDataAdapter(this.UpdStustrconnName,UpdSconn);
SqlComm=new SqlCommandBuilder(UpdStuGrdsqlda);
UpdStuGrdsqlda.Update(this.UpdNameStudset.GetChanges(),"student");
MessageBox.Show("保存修改成功!","信息");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(),"信息");
}
}
//删除学生记录
private void DelRecord_Click(object sender, System.EventArgs e)
{
int rowNumber=this.UpdStuGrdDg.CurrentCell.RowNumber;
try
{
this.UpdClassStudset.Tables[0].Rows[rowNumber].Delete();
this.UpdNameStudset.Tables[0].Rows[rowNumber].Delete();
UpdSconn=new SqlConnection(UpdStuconnstr);
UpdStuGrdsqlda=new SqlDataAdapter("",UpdSconn);
SqlComm=new SqlCommandBuilder(UpdStuGrdsqlda);
UpdStuGrdsqlda.Update(this.UpdNameStudset.GetChanges(),"student");
UpdStuGrdsqlda.Update(this.UpdClassStudset.GetChanges(),"student");
MessageBox.Show("删除成功!","信息");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(),"信息");
}
}