本文介绍了在ASP.net环境下采用“自上而下地总体规划,自下而上地应用开发”的策略开发一个管理信息系统的过程。通过分析某一学校学生管理的不足,创建了一套行之有效的计算机管理学生的方案。文章介绍了学生管理信息系统的系统分析部分,包括可行性分析、业务流程分析等;系统设计部分主要介绍了系统功能设计和数据库设计及代码设计;系统实现部分说明了几个主要模块的算法, 本系统界面友好,操作简单,比较实用。
【关键字】:管理信息系统、学生管理、ASP.net应用
3.3系统功能模块分析
SMS主要的功能包括以下几个方面:用户管理(管理员,老师管理和学生管理),课程管理,班级管理,成绩管理选课管理。设计图如5--3所示,除此之外,系统还包含了教师管理,班级管理等辅助功能模块,用于协调4个主模块。
用户管理模块:该模块由两个子模块构成,分别示管理员与教师管理模块和学生管理模块。主要负责管理员与教师以及学生信息的管理功能,具体参看管理员与教师管理子模块和学生管理子模块的模块功能描述。
管理员与教师管理子模块:该模块主要负责管理系统管理员的信息。它为用户管理模块的一个子模块。主要功能是将本校的教师权限设为管理员。管理员课添加新教师信息,每个教师有唯一的编号,之后通过把教师加为管理员,而令此教师具有管理员的权限,从而教师可以登陆系统进行管理员的相关操作。
学生管理子模块:该模块主要负责管理所有在校注册学生的个人信息。它为用户管理模块的一个子模块。主要功能包括添加,删除,修改,查找学生信息。每个学生有唯一的学号,管理员添加新生后,新生即可登陆此系统浏览个人信息,登陆此系统的用户名和密码默认都是此学生的学号。
课程管理模块:该模块负责管理本校所有的课程信息。主要功能包括添加,删除,修改,查找课程信息。只有管理员才具有对课程信息进行维护的权限,课程的类型分三种:公共课,专业课和选修课。课程管理模块是选课管理模块的基础,只有在课程管理中添有选修课的信息,学生才能进行选课。
选课管理模块:该模块负责选课的管理。主要功能包括删除,统计学生选课信息。它以在课程管理系统中维护号的信息作为基础,既可以对选修课程进行管理,也可以对统计选课人数,并根据学生选课时间先后决定最终选修此课的同学。
成绩管理模块:学生学的每一门课显然最后要有成绩,查询的内肉包括课程名称,学分,成绩等。只有管理员可以录入学生每一门课的成绩,并能进行修改,学生只能查询自己所学课程的成绩,并且可以查询每一学期学生所学课程所获总学分,前提是成绩必须及格,否则将没有此课的成绩。
班级管理模块:该模块负责班级的管理。主要功能包括添加,删除和修改班级信息,以及对班级信息的查询。只有管理员才具有对班级管理信息进行维护的权限。学生信息的添加是建立在班级信息维护的基础上,每个学生必然属于特定的班级。并且在管理员对学生成绩查询统计时,可以统计各个班级的平均分,最高分。
8.1学生管理系统首页
学生管理系统首页具有自动导航功能,不同用户登陆时将根据不同的身份进入不同的系统功能页。
代码入下
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
namespace sms
{
public class _default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox Tbx_userid;
protected System.Web.UI.WebControls.TextBox Tbx_userpwd;
protected System.Web.UI.WebControls.Label Lbl_note;
protected System.Web.UI.WebControls.Button Btn_enter;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Btn_enter.Click += new System.EventHandler(this.Btn_enter_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Btn_enter_Click(object sender, System.EventArgs e)
{
string strconn= ConfigurationSettings.AppSettings["dsn"];
SqlConnection cn=new SqlConnection(strconn);
cn.Open();
string strsql="select * from users where User_id='"+Tbx_userid.Text+"'and User_password='"+Tbx_userpwd.Text+"'";
SqlCommand cm=new SqlCommand(strsql,cn);
SqlDataReader dr=cm.ExecuteReader();
if(dr.Read())
{ Session["User_id"]=dr["User_id"];
Session["user_power"]=dr["User_power"];
if((int)Session["User_power"]==0)
{
Response.Redirect("query.aspx");
}
else
{
Response.Redirect("student.aspx");
}
}
else
{
Lbl_note.Text="对不起,登陆失败!";
}
cn.Close();
}
}
}
8.2学生信息维护页面
学生信息维护模块时SMS重管理学生学籍的部分。页面采用DataGrid控件的Dgd_student与DataSet数据集的绑定返回所有学生信息,可以对学生信息进行修改和删除。
代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
namespace sms
{
/// <summary>
/// student 的摘要说明。
/// </summary>
public class student : System.Web.UI.Page
{
protected System.Web.UI.WebControls.LinkButton Lbtn_course;
protected System.Web.UI.WebControls.LinkButton Lbtn_sortcourse;
protected System.Web.UI.WebControls.DataGrid Dgd_student;
protected System.Web.UI.WebControls.Button Btn_all;
protected System.Web.UI.WebControls.Panel Pnl_sort;
protected System.Web.UI.WebControls.TextBox Tbx_sortid;
protected System.Web.UI.WebControls.Button Btn_ok;
SqlConnection cn;
protected System.Web.UI.WebControls.Button Btn_search;
protected System.Web.UI.WebControls.Label Lbl_note;
protected System.Web.UI.WebControls.LinkButton Lbtn_add;
protected System.Web.UI.WebControls.TextBox Tbx_name;
protected System.Web.UI.WebControls.Button Btn_exit;
protected System.Web.UI.WebControls.LinkButton Lbtn_grade;
protected System.Web.UI.WebControls.LinkButton Lbtn_addclass;
protected System.Web.UI.WebControls.LinkButton Lbtn_addteacher;
protected System.Web.UI.WebControls.LinkButton Lbtn_student;
string strsql;
private void Page_Load(object sender, System.EventArgs e)
{
string strconn= ConfigurationSettings.AppSettings["dsn"];
cn=new SqlConnection(strconn);
if(!IsPostBack)
Bindgrid();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Lbtn_addclass.Click += new System.EventHandler(this.Lbtn_addclass_Click);
this.Lbtn_addteacher.Click += new System.EventHandler(this.Lbtn_addteacher_Click);
this.Lbtn_course.Click += new System.EventHandler(this.Lbtn_course_Click);
this.Lbtn_sortcourse.Click += new System.EventHandler(this.Lbtn_sortcourse_Click);
this.Lbtn_grade.Click += new System.EventHandler(this.Lbtn_grade_Click);
this.Btn_search.Click += new System.EventHandler(this.Btn_search_Click);
this.Btn_ok.Click += new System.EventHandler(this.Btn_ok_Click);
this.Lbtn_add.Click += new System.EventHandler(this.Lbtn_add_Click);
this.Btn_all.Click += new System.EventHandler(this.Btn_all_Click);
this.Btn_exit.Click += new System.EventHandler(this.Btn_exit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Btn_search_Click(object sender, System.EventArgs e)
{
Pnl_sort.Visible=true;
}
private void Btn_all_Click(object sender, System.EventArgs e)
{
Pnl_sort.Visible=false;
Bindgrid();
}
public void DataGrid_Page(object sender,DataGridPageChangedEventArgs e)
{
Dgd_student.CurrentPageIndex=e.NewPageIndex;
Bindgrid();
}
public void DataGrid_cancel(object sender,DataGridCommandEventArgs e)
{
Dgd_student.EditItemIndex=-1;
Bindgrid();
}
public void DataGrid_edit(object sender,DataGridCommandEventArgs e)
{
Dgd_student.EditItemIndex=(int)e.Item.ItemIndex;
Bindgrid();
}
public void DataGrid_update(object sender,DataGridCommandEventArgs e)
{
string strsql="update student set Student_name=@Student_name,Student_sex=@Student_sex,Student_nation=@Student_nation,Student_birthday=@Student_birthday,Student_time=@Student_time,Student_classid=@Student_classid,Student_home=@Student_home,Student_else=@Student_else where Student_id=@Student_id";
SqlCommand cm=new SqlCommand(strsql,cn);
try
{
cm.Parameters.Add(new SqlParameter("@Student_name",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_sex",SqlDbType.Char,10));
cm.Parameters.Add(new SqlParameter("@Student_nation",SqlDbType.Char,10));
cm.Parameters.Add(new SqlParameter("@Student_birthday",SqlDbType.DateTime,8));
cm.Parameters.Add(new SqlParameter("@Student_time",SqlDbType.DateTime,8));
cm.Parameters.Add(new SqlParameter("@Student_classid",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_home",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_else",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_id",SqlDbType.VarChar,50));
string colvalue=((TextBox)e.Item.Cells[1].Controls[0]).Text;
cm.Parameters["@Student_name"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[2].Controls[0]).Text;
cm.Parameters["@Student_sex"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[3].Controls[0]).Text;
cm.Parameters["@Student_nation"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[4].Controls[0]).Text;
cm.Parameters["@Student_birthday"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[5].Controls[0]).Text;
cm.Parameters["@Student_time"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[6].Controls[0]).Text;
cm.Parameters["@Student_classid"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[7].Controls[0]).Text;
cm.Parameters["@Student_home"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[8].Controls[0]).Text;
cm.Parameters["@Student_else"].Value=colvalue;
cm.Parameters["@Student_id"].Value=Dgd_student.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
cm.ExecuteNonQuery();
Lbl_note.Text="编辑成功";
Dgd_student.EditItemIndex=-1;
}
catch
{
Lbl_note.Text="编辑失败,请检查输入!";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
Bindgrid();
}
public void DataGrid_delete(object sender,DataGridCommandEventArgs e)
{
string strsql="delete from student where Student_id=@userid";
SqlCommand cm=new SqlCommand(strsql,cn);
cm.Parameters.Add(new SqlParameter("@userid",SqlDbType.VarChar,50));
cm.Parameters["@userid"].Value=Dgd_student.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="删除成功";
}
catch(SqlException)
{
Lbl_note.Text="删除失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
Bindgrid();
}
public void Bindgrid()
{ strsql="select * from student";
SqlDataAdapter da=new SqlDataAdapter(strsql,cn);
DataSet ds=new DataSet();
da.Fill(ds);
Dgd_student.DataSource=ds;
Dgd_student.DataBind();
}
private void Btn_ok_Click(object sender, System.EventArgs e)
{
strsql="select * from student where Student_id='"+Tbx_sortid.Text+"'or Student_name='"+Tbx_name.Text+"'";
SqlDataAdapter da=new SqlDataAdapter(strsql,cn);
DataSet ds=new DataSet();
da.Fill(ds);
Dgd_student.DataSource=ds;
Dgd_student.DataBind();
}
private void Lbtn_course_Click(object sender, System.EventArgs e)
{
Response.Redirect("course.aspx");
}
private void Lbtn_sortcourse_Click(object sender, System.EventArgs e)
{
Response.Redirect("student_course.aspx");
}
private void Lbtn_add_Click(object sender, System.EventArgs e)
{
Response.Redirect("addstudent.aspx");
}
private void Btn_exit_Click(object sender, System.EventArgs e)
{
Response.Redirect("default.aspx");
}
private void Lbtn_grade_Click(object sender, System.EventArgs e)
{
Response.Redirect("grade_manage.aspx");
}
private void Lbtn_addclass_Click(object sender, System.EventArgs e)
{
Response.Redirect("classes.aspx");
}
private void Lbtn_addteacher_Click(object sender, System.EventArgs e)
{
Response.Redirect("teacher.aspx");
}
private void Lbtn_student_Click(object sender, System.EventArgs e)
{
Response.Redirect("student.aspx");
}
}
}
8.3学生信息添加页面
学生信息添加页面主要负责往数据库里添加数据。后台支持事件主要为Btn_ok_Click()事件负责当用户单击“确定”按钮后的数据库添加操作。利用RequiredFieldValidator控件可以用来验证用户输入信息是否为空。若输入学号存在,利用CustomValidator验证控件,触发ServerValidator事件,判断是否存在此学号的学生。“重置”按钮的click事件将完成刷新此页面的功能。
代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
namespace sms
{
/// <summary>
/// addstudent 的摘要说明。
/// </summary>
public class addstudent : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox Tbx_id;
protected System.Web.UI.WebControls.TextBox Tbx_name;
protected System.Web.UI.WebControls.TextBox Tbx_nation;
protected System.Web.UI.WebControls.TextBox Tbx_birthday;
protected System.Web.UI.WebControls.TextBox Tbx_time;
protected System.Web.UI.WebControls.TextBox Tbx_home;
protected System.Web.UI.WebControls.TextBox Tbx_else;
protected System.Web.UI.WebControls.DropDownList Ddl_sex;
protected System.Web.UI.WebControls.Button Btn_ok;
protected System.Web.UI.WebControls.CustomValidator Cv_id;
protected System.Web.UI.WebControls.Label Lbl_note;
protected System.Web.UI.WebControls.Button Btn_reset;
protected System.Web.UI.WebControls.RequiredFieldValidator Rfv_name;
protected System.Web.UI.WebControls.RequiredFieldValidator Rfv_id;
protected System.Web.UI.WebControls.RequiredFieldValidator Rfv_nation;
protected System.Web.UI.WebControls.RequiredFieldValidator Rfv_birthday;
protected System.Web.UI.WebControls.RequiredFieldValidator Rfv_time;
protected System.Web.UI.WebControls.RequiredFieldValidator Rfv_class;
protected System.Web.UI.WebControls.RequiredFieldValidator Rfv_home;
protected System.Web.UI.WebControls.DropDownList Ddl_class;
SqlConnection cn;
private void Page_Load(object sender, System.EventArgs e)
{//班级名称下拉列表框绑定
string strconn= ConfigurationSettings.AppSettings["dsn"];
SqlConnection cn0=new SqlConnection(strconn);
if(!IsPostBack)
{
cn0.Open ();
string mysql="select * from class";
SqlCommand cm1=new SqlCommand (mysql,cn0);
SqlDataReader dr1=cm1.ExecuteReader ();
while(dr1.Read ())
{
Ddl_class.Items .Add (new ListItem(dr1["Class_name"].ToString() ,dr1["Class_id"].ToString()) );
}
cn0.Close ();
}
// 在此处放置用户代码以初始化页面
cn=new SqlConnection(strconn);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Cv_id.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.Cv_id_ServerValidate);
this.Btn_ok.Click += new System.EventHandler(this.Btn_ok_Click);
this.Btn_reset.Click += new System.EventHandler(this.Btn_reset_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Btn_ok_Click(object sender, System.EventArgs e)
{
SqlCommand cm=new SqlCommand("insert_student_1",cn);
cm.CommandType=CommandType.StoredProcedure;
try
{
cm.Parameters.Add(new SqlParameter("@Student_id",SqlDbType.VarChar,50));
cm.Parameters["@Student_id"].Value=Tbx_id.Text;
cm.Parameters.Add(new SqlParameter("@Student_name",SqlDbType.VarChar,50));
cm.Parameters["@Student_name"].Value=Tbx_name.Text;
cm.Parameters.Add(new SqlParameter("@Student_sex",SqlDbType.Char,10));
cm.Parameters["@Student_sex"].Value=Ddl_sex.SelectedItem.Value;
cm.Parameters.Add(new SqlParameter("@Student_nation",SqlDbType.Char,10));
cm.Parameters["@Student_nation"].Value=Tbx_nation.Text;
cm.Parameters.Add(new SqlParameter("@Student_birthday",SqlDbType.DateTime,8));
cm.Parameters["@Student_birthday"].Value=Tbx_birthday.Text;
cm.Parameters.Add(new SqlParameter("@Student_time",SqlDbType.DateTime,8));
cm.Parameters["@Student_time"].Value=Tbx_time.Text;
cm.Parameters.Add(new SqlParameter("@Student_classid",SqlDbType.VarChar,50));
cm.Parameters["@Student_classid"].Value=Ddl_class.SelectedItem.Value;
cm.Parameters.Add(new SqlParameter("@Student_home",SqlDbType.VarChar,50));
cm.Parameters["@Student_home"].Value=Tbx_home.Text;
cm.Parameters.Add(new SqlParameter("@Student_else",SqlDbType.VarChar,50));
cm.Parameters["@Student_else"].Value=Tbx_else.Text;
cm.Connection.Open();
cm.ExecuteNonQuery();
cm.Connection.Close();
insert();
}
catch
{
Lbl_note.Text="添加失败,请检查输入!";
Lbl_note.Style["color"]="red";
}
}
public void insert()
{
string strsl="insert into users(User_id,User_password,User_power) values(@User_id,@User_pwd,0)";
SqlCommand cm=new SqlCommand(strsl,cn);
cm.Parameters.Add(new SqlParameter("@User_id",SqlDbType.VarChar,50));
cm.Parameters["@User_id"].Value=Tbx_id.Text;
cm.Parameters.Add(new SqlParameter("@User_pwd",SqlDbType.VarChar,50));
cm.Parameters["@User_pwd"].Value=Tbx_id.Text;
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Response.Redirect("student.aspx");
}
catch(SqlException)
{
Lbl_note.Text="添加失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
}
private void Btn_reset_Click(object sender, System.EventArgs e)
{
Response.Redirect("addstudent.aspx");
}
private void Cv_id_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string strconn= ConfigurationSettings.AppSettings["dsn"];
cn=new SqlConnection(strconn);
cn.Open();
SqlCommand cm=new SqlCommand("select_student_1",cn);
cm.CommandType=CommandType.StoredProcedure;
cm.Parameters.Add("@Student_id",SqlDbType.VarChar,50);
cm.Parameters["@Student_id"].Value=Tbx_id.Text;
SqlDataReader dr=cm.ExecuteReader();
if(dr.Read())
{
args.IsValid=false;
}
else
{
args.IsValid=true;
}
cn.Close();
}
}
}
8.4课程信息管理页面
课程信息管理页面与学生信息维护页面非常相似,进行DataGrid控件Dgd-course的绑定操作,管理员可以在此页面进行课程信息的编辑和删除。
主要相关代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
namespace sms
{
/// <summary>
/// course 的摘要说明。
/// </summary>
public class course : System.Web.UI.Page
{
SqlConnection cn;
protected System.Web.UI.WebControls.TextBox Tbx_name;
protected System.Web.UI.WebControls.TextBox Tbx_id;
protected System.Web.UI.WebControls.Button Btn_search;
protected System.Web.UI.WebControls.DataGrid Dgd_course;
protected System.Web.UI.WebControls.Label Lbl_note;
protected System.Web.UI.WebControls.Button Btn_exit;
string strsql;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string strconn= ConfigurationSettings.AppSettings["dsn"];
cn=new SqlConnection(strconn);
if(!IsPostBack)
Bindgrid();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Btn_search.Click += new System.EventHandler(this.Btn_search_Click);
this.Btn_exit.Click += new System.EventHandler(this.Btn_exit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
public void DataGrid_Page(object sender,DataGridPageChangedEventArgs e)
{
Dgd_course.CurrentPageIndex=e.NewPageIndex;
Bindgrid();
}
public void DataGrid_edit(object sender,DataGridCommandEventArgs e)
{
Dgd_course.EditItemIndex=(int)e.Item.ItemIndex;
Bindgrid();
}
public void DataGrid_cancel(object sender,DataGridCommandEventArgs e)
{
Dgd_course.EditItemIndex=-1;
Bindgrid();
}
public void DataGrid_delete(object sender,DataGridCommandEventArgs e)
{
string strsql="delete from course where Course_id=@course_id";
SqlCommand cm=new SqlCommand(strsql,cn);
cm.Parameters.Add(new SqlParameter("@course_id",SqlDbType.VarChar,50));
cm.Parameters["@course_id"].Value=Dgd_course.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="删除成功";
}
catch(SqlException)
{
Lbl_note.Text="删除失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
Bindgrid();
}
public void DataGrid_update(object sender,DataGridCommandEventArgs e)
{
string strsql="update course set Course_name=@Course_name,Course_period=@Course_period,Course_credit=@Course_credit,Course_kind=@Course_kind where Course_id=@Course_id";
SqlCommand cm=new SqlCommand(strsql,cn);
cm.Parameters.Add(new SqlParameter("@Course_name",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Course_period",SqlDbType.Int,4));
cm.Parameters.Add(new SqlParameter("@Course_credit",SqlDbType.Int,4));
cm.Parameters.Add(new SqlParameter("@Course_kind",SqlDbType.Int,4));
cm.Parameters.Add(new SqlParameter("@Course_id",SqlDbType.VarChar,50));
string colvalue=((TextBox)e.Item.Cells[1].Controls[0]).Text;
cm.Parameters["@Course_name"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[2].Controls[0]).Text;
cm.Parameters["@Course_period"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[3].Controls[0]).Text;
cm.Parameters["@Course_credit"].Value=colvalue;
colvalue=((TextBox)e.Item.Cells[4].Controls[0]).Text;
cm.Parameters["@Course_kind"].Value=colvalue;
cm.Parameters["@Course_id"].Value=Dgd_course.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="编辑成功";
Dgd_course.EditItemIndex=-1;
}
catch(SqlException)
{
Lbl_note.Text="编辑失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
Bindgrid();
}
public void Bindgrid()
{
strsql="select * from course";
SqlDataAdapter da=new SqlDataAdapter(strsql,cn);
DataSet ds=new DataSet();
da.Fill(ds);
Dgd_course.DataSource=ds;
Dgd_course.DataBind();
}
private void Lbtn_addcourse_Click(object sender, System.EventArgs e)
{
Response.Redirect("addcourse.aspx");
}
private void Btn_exit_Click(object sender, System.EventArgs e)
{
Response.Redirect("default.aspx");
}
private void Btn_search_Click(object sender, System.EventArgs e)
{
strsql="select * from course where Course_id='"+Tbx_id.Text+"'or Course_name='"+Tbx_name.Text+"'";
SqlDataAdapter da=new SqlDataAdapter(strsql,cn);
DataSet ds=new DataSet();
da.Fill(ds);
Dgd_course.DataSource=ds;
Dgd_course.DataBind();
}
private void Lbtn_assign_Click(object sender, System.EventArgs e)
{
Response.Redirect("assigncourse.aspx");
}
}
}
8.5录入成绩页面
在录入成绩页面的初始化事件Page_Load()事件对“课程”下拉列表框控件Ddl_course进行初始绑定,使其显示该学生已选的所有课程信息。“录入”按钮的Click事件调用数据库操作语句,对数据库中相应学生,成绩关系表中的成绩信息进行更新。
主要相关代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
namespace sms
{
/// <summary>
/// addgrade 的摘要说明。
/// </summary>
public class addgrade : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox Tbx_studentid;
protected System.Web.UI.WebControls.Button Btn_add;
protected System.Web.UI.WebControls.Label Lbl_note;
protected System.Web.UI.WebControls.Button Btn_exit;
protected System.Web.UI.WebControls.TextBox Tbx_term;
protected System.Web.UI.WebControls.CustomValidator Cv_id;
protected System.Web.UI.WebControls.DropDownList Ddl_course;
protected System.Web.UI.WebControls.TextBox Tbx_grade;
SqlConnection cn;
private void Page_Load(object sender, System.EventArgs e)
{
string strconn= ConfigurationSettings.AppSettings["dsn"];
// 在此处放置用户代码以初始化页面
cn=new SqlConnection(strconn);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Tbx_studentid.TextChanged += new System.EventHandler(this.Tbx_studentid_TextChanged);
this.Cv_id.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.Cv_id_ServerValidate);
this.Btn_add.Click += new System.EventHandler(this.Btn_add_Click);
this.Btn_exit.Click += new System.EventHandler(this.Btn_exit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Btn_add_Click(object sender, System.EventArgs e)
{
string strsql="update student_course set Student_grade=@Student_grade where Student_id=@Student_id and Course_id=@Course_id and Course_year=@Course_year";
SqlCommand cm=new SqlCommand(strsql,cn);
cm.Parameters.Add(new SqlParameter("@Student_id",SqlDbType.VarChar,50));
cm.Parameters["@Student_id"].Value=Tbx_studentid.Text;
cm.Parameters.Add(new SqlParameter("@Course_id",SqlDbType.VarChar,50));
cm.Parameters["@Course_id"].Value=Ddl_course.SelectedItem.Value;
cm.Parameters.Add(new SqlParameter("@Course_year",SqlDbType.Char,10));
cm.Parameters["@Course_year"].Value=Tbx_term.Text;
cm.Parameters.Add(new SqlParameter("@Student_grade",SqlDbType.Int,4));
cm.Parameters["@Student_grade"].Value=Tbx_grade.Text;
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Response.Redirect("grade_manage.aspx");
}
catch(SqlException)
{
Lbl_note.Text="添加失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
}
private void Btn_exit_Click(object sender, System.EventArgs e)
{
Response.Redirect("grade_manage.aspx");
}
private void Cv_id_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string strconn= ConfigurationSettings.AppSettings["dsn"];
cn=new SqlConnection(strconn);
cn.Open();
string strsql="select * from student where Student_id='"+Tbx_studentid.Text+"'";
SqlCommand cm=new SqlCommand(strsql,cn);
SqlDataReader dr=cm.ExecuteReader();
if(dr.Read())
{
args.IsValid=true;
}
else
{
args.IsValid=false;
}
cn.Close();
}
private void Tbx_studentid_TextChanged(object sender, System.EventArgs e)
{ //课程名称下拉列表框绑定
string strconn= ConfigurationSettings.AppSettings["dsn"];
SqlConnection cn0=new SqlConnection(strconn);
cn0.Open ();
string mysql1="select course.* from student_course,course where student_course.Student_id='"+Tbx_studentid.Text+"'and student_course.Course_id=course.Course_id";
SqlCommand cm1=new SqlCommand (mysql1,cn0);
SqlDataReader dr1=cm1.ExecuteReader ();
while(dr1.Read ())
{
Ddl_course.Items .Add (new ListItem(dr1["Course_name"].ToString() ,dr1["Course_id"].ToString()) );
}
cn0.Close ();
}
}
}
8.6添加学生必修课页面
在添加学生必修课页面初始化事件Page_Load事件中将数据库中的所有课程及班级信息分别绑定到DropDownList控件的Ddl_course和Ddl_class控件中,“确定”按钮Btn_ok控件的Click()事件完成往数据库学生-课程信息表中添加所选班级范围下所有学生的相应课程选课记录的任务。
主要相关代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace sms
{
/// <summary>
/// addstudentcourse 的摘要说明。
/// </summary>
public class addstudentcourse : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList Ddl_course;
protected System.Web.UI.WebControls.DropDownList Ddl_class;
protected System.Web.UI.WebControls.TextBox Tbx_year;
protected System.Web.UI.WebControls.Button Btn_ok;
protected System.Web.UI.WebControls.Label Lbl_note;
protected System.Web.UI.WebControls.Button Btn_back;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string strconn= ConfigurationSettings.AppSettings["dsn"];
//连接本地计算机的sms数据库
SqlConnection cn0= new SqlConnection (strconn);
if(!IsPostBack)
{
//课程名称下拉列表框绑定
cn0.Open ();
string mysql1="select * from course where Course_kind='0'or Course_kind='1'";
SqlCommand cm1=new SqlCommand (mysql1,cn0);
SqlDataReader dr1=cm1.ExecuteReader ();
while(dr1.Read ())
{
Ddl_course.Items .Add (new ListItem(dr1["Course_name"].ToString() ,dr1["Course_id"].ToString()) );
}
cn0.Close ();
//班级名称下拉列表框绑定
cn0.Open ();
string mysql2="select * from class";
SqlCommand cm2=new SqlCommand (mysql2,cn0);
SqlDataReader dr2=cm2.ExecuteReader ();
while(dr2.Read ())
{
Ddl_class.Items .Add (new ListItem(dr2["Class_name"].ToString() ,dr2["Class_id"].ToString()) );
}
cn0.Close ();
}
}
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Btn_ok.Click += new System.EventHandler(this.Btn_ok_Click);
this.Btn_back.Click += new System.EventHandler(this.Btn_back_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Btn_ok_Click(object sender, System.EventArgs e)
{
string strconn= ConfigurationSettings.AppSettings["dsn"];
SqlConnection cn= new SqlConnection (strconn);
cn.Open();
string strsql="insert into student_course(Student_id,Course_id,Course_year) (select Student_id,@Course_id,@Course_year from student where Student_classid=@classid) ";
SqlCommand cm=new SqlCommand(strsql,cn);
cm.Parameters.Add(new SqlParameter("@classid",SqlDbType.VarChar,50));
cm.Parameters["@classid"].Value=Ddl_class.SelectedItem.Value;
cm.Parameters.Add(new SqlParameter("@Course_id",SqlDbType.VarChar,50));
cm.Parameters["@Course_id"].Value=Ddl_course.SelectedItem.Value;
cm.Parameters.Add(new SqlParameter("@Class_id",SqlDbType.VarChar,50));
cm.Parameters["@Class_id"].Value=Ddl_class.SelectedItem.Value;
cm.Parameters.Add(new SqlParameter("@Course_year",SqlDbType.VarChar,50));
cm.Parameters["@Course_year"].Value=Tbx_year.Text;
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="添加成功,请返回";
}
catch(SqlException)
{
Lbl_note.Text="添加失败";
Lbl_note.Style["color"]="red";
}
cn.Close();
}
private void Btn_back_Click(object sender, System.EventArgs e)
{
Response.Redirect("course.aspx");
}
}
}
8.7成绩管理页面
成绩管理页面完成的功能比较多,包括按选定的条件进行限定条件的成绩查询。同时,也可以根据成绩范围对包含在该范围中的学生成绩作一统计,具体统计这门课的平均分,最高分,参加考试总人数以及优秀人数和不及格人数。此页面的关键就在于根据条件生成SQL语句。当“查询”“统计”操作被触发,系统将完成对数据库中多个表 的操作。
主要相关代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
namespace sms
{
/// <summary>
/// grade_manage 的摘要说明。
/// </summary>
public class grade_manage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList Ddl_way;
protected System.Web.UI.WebControls.TextBox Tbx_name;
protected System.Web.UI.WebControls.Button Btn_search;
protected System.Web.UI.WebControls.DataGrid Dgd_grade;
protected System.Web.UI.WebControls.LinkButton Link_addgrade;
SqlConnection cn;
protected System.Web.UI.WebControls.Label Lbl_note;
protected System.Web.UI.WebControls.DropDownList Ddl_stat;
protected System.Web.UI.WebControls.TextBox Tbx_area;
protected System.Web.UI.WebControls.TextBox Tbx_classid;
protected System.Web.UI.WebControls.DropDownList Ddl_content;
protected System.Web.UI.WebControls.Button Btn_count;
protected System.Web.UI.WebControls.TextBox Tbx_year;
protected System.Web.UI.WebControls.Label Lbl_average;
protected System.Web.UI.WebControls.Label Lbl_high;
protected System.Web.UI.WebControls.Label Lbl_a;
protected System.Web.UI.WebControls.Label Lbl_unpass;
protected System.Web.UI.WebControls.Label Lbl_all;
protected System.Web.UI.WebControls.TextBox Tbx_term;
string strsql;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string strconn= ConfigurationSettings.AppSettings["dsn"];
cn=new SqlConnection(strconn);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Btn_count.Click += new System.EventHandler(this.Btn_count_Click);
this.Btn_search.Click += new System.EventHandler(this.Btn_search_Click);
this.Link_addgrade.Click += new System.EventHandler(this.Link_addgrade_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Link_addgrade_Click(object sender, System.EventArgs e)
{
Response.Redirect("addgrade.aspx");
}
private void Btn_search_Click(object sender, System.EventArgs e)
{
Bindgrid();
}
public void DataGrid_Page(object sender,DataGridPageChangedEventArgs e)
{
Dgd_grade.CurrentPageIndex=e.NewPageIndex;
Bindgrid();
}
public void Bindgrid()
{
if(Ddl_way.SelectedItem.Text=="按学号")
{
if(Tbx_term.Text=="所有")
{
strsql="select student_course.ID,student_course.Student_id,student.Student_name,student.Student_classid,student_course.Course_id,course.Course_name,teacher.Teacher_name,student_course.Student_grade from course,student,teacher,student_course,teacher_course where student_course.Student_id='"+Tbx_name.Text+"' and student_course.Course_id=course.Course_id and student_course.Student_id=student.Student_id and student_course.Course_id=teacher_course.Course_id and teacher_course.Teacher_id=teacher.Teacher_id and teacher_course.Class_id=student.Student_classid";
}
else
{
strsql="select student_course.ID,student_course.Student_id,student.Student_name,student.Student_classid,student_course.Course_id,course.Course_name,teacher.Teacher_name,student_course.Student_grade from course,student,teacher,student_course,teacher_course where student_course.Student_id='"+Tbx_name.Text+"' and Course_year='"+Tbx_term.Text+"' and student_course.Course_id=course.Course_id and student_course.Student_id=student.Student_id and student_course.Course_id=teacher_course.Course_id and teacher_course.Teacher_id=teacher.Teacher_id and teacher_course.Class_id=student.Student_classid";
}
}
else if(Ddl_way.SelectedItem.Text=="按课号")
{
if(Tbx_term.Text=="所有")
{
strsql="select student_course.ID,student_course.Student_id,student.Student_name,student.Student_classid,student_course.Course_id,course.Course_name,teacher.Teacher_name,student_course.Student_grade from course,student,teacher,student_course,teacher_course where student_course.Course_id='"+Tbx_name.Text+"' and student_course.Course_id=course.Course_id and student_course.Student_id=student.Student_id and student_course.Course_id=teacher_course.Course_id and teacher_course.Teacher_id=teacher.Teacher_id and teacher_course.Class_id=student.Student_classid";
}
else
{
strsql="select student_course.ID,student_course.Student_id,student.Student_name,student.Student_classid,student_course.Course_id,course.Course_name,teacher.Teacher_name,student_course.Student_grade from course,student,teacher,student_course,teacher_course where student_course.Course_id='"+Tbx_name.Text+"' and student_course.Course_year='"+Tbx_term.Text+"' and student_course.Course_id=course.Course_id and student_course.Student_id=student.Student_id and student_course.Course_id=teacher_course.Course_id and teacher_course.Teacher_id=teacher.Teacher_id and teacher_course.Class_id=student.Student_classid";
}
}
else if(Ddl_way.SelectedItem.Text=="按课程名")
{
if(Tbx_term.Text=="所有")
{
strsql="select student_course.ID,student_course.Student_id,student.Student_name,student.Student_classid,student_course.Course_id,course.Course_name,teacher.Teacher_name,student_course.Student_grade from course,student,teacher,student_course,teacher_course where course.Course_name='"+Tbx_name.Text+"' and student_course.Course_id=course.Course_id and student_course.Student_id=student.Student_id and student_course.Course_id=teacher_course.Course_id and teacher_course.Teacher_id=teacher.Teacher_id and teacher_course.Class_id=student.Student_classid";
}
else
{
strsql="select student_course.ID,student_course.Student_id,student.Student_name,student.Student_classid,student_course.Course_id,course.Course_name,teacher.Teacher_name,student_course.Student_grade from course,student,teacher,student_course,teacher_course where course.Course_name='"+Tbx_name.Text+"' and student_course.Course_year='"+Tbx_term.Text+"' and student_course.Course_id=course.Course_id and student_course.Student_id=student.Student_id and student_course.Course_id=teacher_course.Course_id and teacher_course.Teacher_id=teacher.Teacher_id and teacher_course.Class_id=student.Student_classid";
}
}
else if(Ddl_way.SelectedItem.Text=="按班级")
{
if(Tbx_term.Text=="所有")
{
strsql="select student_course.ID,student_course.Student_id,student.Student_name,student.Student_classid,student_course.Course_id,course.Course_name,teacher.Teacher_name,student_course.Student_grade from course,student,teacher,student_course,teacher_course where student.Student_classid='"+Tbx_name.Text+"'and student_course.Course_id=course.Course_id and student_course.Student_id=student.Student_id and student_course.Course_id=teacher_course.Course_id and teacher_course.Teacher_id=teacher.Teacher_id and teacher_course.Class_id=student.Student_classid";
}
else
{
strsql="select student_course.ID,student_course.Student_id,student.Student_name,student.Student_classid,student_course.Course_id,course.Course_name,teacher.Teacher_name,student_course.Student_grade from course,student,teacher,student_course,teacher_course where student.Student_classid='"+Tbx_name.Text+"' and student_course.Course_year='"+Tbx_term.Text+"' and student_course.Course_id=course.Course_id and student_course.Student_id=student.Student_id and student_course.Course_id=teacher_course.Course_id and teacher_course.Teacher_id=teacher.Teacher_id and teacher_course.Class_id=student.Student_classid";
}
}
else if(Ddl_way.SelectedItem.Text=="按教师号")
{
if(Tbx_term.Text=="所有")
{
strsql="select student_course.ID,student_course.Student_id,student.Student_name,student.Student_classid,student_course.Course_id,course.Course_name,teacher.Teacher_name,student_course.Student_grade from course,student,teacher,student_course,teacher_course where teacher.Teacher_id='"+Tbx_name.Text+"' and student_course.Course_id=course.Course_id and student_course.Student_id=student.Student_id and student_course.Course_id=teacher_course.Course_id and teacher_course.Teacher_id=teacher.Teacher_id and teacher_course.Class_id=student.Student_classid";
}
else
{
strsql="select student_course.ID,student_course.Student_id,student.Student_name,student.Student_classid,student_course.Course_id,course.Course_name,teacher.Teacher_name,student_course.Student_grade from course,student,teacher,student_course,teacher_course where teacher.Teacher_id='"+Tbx_name.Text+"' and student_course.Course_year='"+Tbx_term.Text+"' and student_course.Course_id=course.Course_id and student_course.Student_id=student.Student_id and student_course.Course_id=teacher_course.Course_id and teacher_course.Teacher_id=teacher.Teacher_id and teacher_course.Class_id=student.Student_classid";
}
}
SqlCommand cm=new SqlCommand(strsql,cn);
cn.Open();
SqlDataReader dr=cm.ExecuteReader();
if(dr.Read())
{
Dgd_grade.Visible=true;
cn.Close();
cn.Open();
SqlDataAdapter da=new SqlDataAdapter(strsql,cn);
DataSet ds=new DataSet();
da.Fill(ds);
Dgd_grade.DataSource=ds;
Dgd_grade.DataBind();
}
else
{
Dgd_grade.Visible=false;
Lbl_note.Text="无纪录!";
}
cn.Close();
}
public void DataGrid_cancel(object sender,DataGridCommandEventArgs e)
{
Dgd_grade.EditItemIndex=-1;
Bindgrid();
}
public void DataGrid_edit(object sender,DataGridCommandEventArgs e)
{
Dgd_grade.EditItemIndex=(int)e.Item.ItemIndex;
Bindgrid();
}
public void DataGrid_update(object sender,DataGridCommandEventArgs e)
{
string strsql="update student_course set Student_grade=@Student_grade where Student_id=@Student_id and Course_id=@Course_id and ID=@id";
SqlCommand cm=new SqlCommand(strsql,cn);
cm.Parameters.Add(new SqlParameter("@Student_grade",SqlDbType.Int,4));
cm.Parameters.Add(new SqlParameter("@Course_id",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Student_id",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@id",SqlDbType.Char,10));
string colvalue=((TextBox)e.Item.Cells[7].Controls[0]).Text;
cm.Parameters["@Student_grade"].Value=colvalue;
cm.Parameters["@Student_id"].Value=e.Item.Cells[1].Text;
cm.Parameters["@Course_id"].Value=e.Item.Cells[4].Text;
cm.Parameters["@Student_grade"].Value=colvalue;
cm.Parameters["@id"].Value=Dgd_grade.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="编辑成功";
Dgd_grade.EditItemIndex=-1;
}
catch(SqlException)
{
Lbl_note.Text="编辑失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
Bindgrid();
}
public void DataGrid_delete(object sender,DataGridCommandEventArgs e)
{
string strsql="delete from student_course where ID=@id";
SqlCommand cm=new SqlCommand(strsql,cn);
cm.Parameters.Add(new SqlParameter("@id",SqlDbType.Char,10));
cm.Parameters["@id"].Value=Dgd_grade.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="删除成功";
}
catch(SqlException)
{
Lbl_note.Text="删除失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
Bindgrid();
}
private void Btn_count_Click(object sender, System.EventArgs e)
{
if(Ddl_stat.SelectedItem.Value=="系别")
{
if(Ddl_content.SelectedItem.Text=="总人数")
{
strsql="select AVG(Student_grade),MAX(Student_grade),COUNT(*) from student_course,student,class where Course_id='"+Tbx_classid.Text+"' and student_course.Course_year='"+Tbx_year.Text+"'and student_course.Student_id=student.Student_id and student.Student_classid=class.Class_id and class.Class_department='"+Tbx_area.Text+"' ";
}
else if(Ddl_content.SelectedItem.Text=="优秀人数")
{
strsql="select AVG(Student_grade),MAX(Student_grade),COUNT(*) from student_course,student,class where Course_id='"+Tbx_classid.Text+"' and student_course.Course_year='"+Tbx_year.Text+"'and student_course.Student_id=student.Student_id and student.Student_classid=class.Class_id and class.Class_department='"+Tbx_area.Text+"'and Student_grade>=85 ";
}
else if(Ddl_content.SelectedItem.Text=="不及格人数")
{
strsql="select AVG(Student_grade),MAX(Student_grade),COUNT(*) from student_course,student,class where Course_id='"+Tbx_classid.Text+"' and student_course.Course_year='"+Tbx_year.Text+"'and student_course.Student_id=student.Student_id and student.Student_classid=class.Class_id and class.Class_department='"+Tbx_area.Text+"'and Student_grade<60 ";
}
else
{
strsql="select AVG(Student_grade),MAX(Student_grade) from student_course,student,class where Course_id='"+Tbx_classid.Text+"' and student_course.Course_year='"+Tbx_year.Text+"'and student_course.Student_id=student.Student_id and student.Student_classid=class.Class_id and class.Class_department='"+Tbx_area.Text+"' ";
}
}
else if(Ddl_stat.SelectedItem.Value=="学院")
{
if(Ddl_content.SelectedItem.Text=="总人数")
{
strsql="select AVG(Student_grade),MAX(Student_grade),COUNT(*) from student_course,student,class where Course_id='"+Tbx_classid.Text+"' and student_course.Course_year='"+Tbx_year.Text+"'and student_course.Student_id=student.Student_id and student.Student_classid=class.Class_id and class.Class_college='"+Tbx_area.Text+"' ";
}
else if(Ddl_content.SelectedItem.Text=="优秀人数")
{
strsql="select AVG(Student_grade),MAX(Student_grade),COUNT(*) from student_course,student,class where Course_id='"+Tbx_classid.Text+"' and student_course.Course_year='"+Tbx_year.Text+"'and student_course.Student_id=student.Student_id and student.Student_classid=class.Class_id and class.Class_college='"+Tbx_area.Text+"'and Student_grade>=85 ";
}
else if(Ddl_content.SelectedItem.Text=="不及格人数")
{
strsql="select AVG(Student_grade),MAX(Student_grade),COUNT(*) from student_course,student,class where Course_id='"+Tbx_classid.Text+"' and student_course.Course_year='"+Tbx_year.Text+"'and student_course.Student_id=student.Student_id and student.Student_classid=class.Class_id and class.Class_college='"+Tbx_area.Text+"'and Student_grade<60 ";
}
else
{
strsql="select AVG(Student_grade),MAX(Student_grade) from student_course,student,class where Course_id='"+Tbx_classid.Text+"' and student_course.Course_year='"+Tbx_year.Text+"'and student_course.Student_id=student.Student_id and student.Student_classid=class.Class_id and class.Class_college='"+Tbx_area.Text+"' ";
}
}
else if(Ddl_stat.SelectedItem.Value=="班级")
{
if(Ddl_content.SelectedItem.Text=="总人数")
{
strsql="select AVG(Student_grade),MAX(Student_grade),COUNT(*) from student_course,student where Course_id='"+Tbx_classid.Text+"' and student_course.Course_year='"+Tbx_year.Text+"' and student_course.Student_id=student.Student_id and student.Student_classid='"+Tbx_area.Text+"'";
}
else if(Ddl_content.SelectedItem.Text=="优秀人数")
{
strsql="select AVG(Student_grade),MAX(Student_grade),COUNT(*) from student_course,student where Course_id='"+Tbx_classid.Text+"' and student_course.Course_year='"+Tbx_year.Text+"' and student_course.Student_id=student.Student_id and student.Student_classid='"+Tbx_area.Text+"'and Student_grade>=85";
}
else if(Ddl_content.SelectedItem.Text=="不及格人数")
{strsql="select AVG(Student_grade),MAX(Student_grade),COUNT(*) from student_course,student where Course_id='"+Tbx_classid.Text+"' and student_course.Course_year='"+Tbx_year.Text+"' and student_course.Student_id=student.Student_id and student.Student_classid='"+Tbx_area.Text+"'and Student_grade<60";
}
else
{
strsql="select AVG(Student_grade),MAX(Student_grade) from student_course,student where Course_id='"+Tbx_classid.Text+"' and student_course.Course_year='"+Tbx_year.Text+"' and student_course.Student_id=student.Student_id and student.Student_classid='"+Tbx_area.Text+"'";
}
}
SqlCommand cm=new SqlCommand(strsql,cn);
cn.Open();
SqlDataReader dr=cm.ExecuteReader();
if(dr.Read())
{
if(Ddl_content.SelectedItem.Text=="均分")
{
Lbl_average.Visible=true;
Lbl_average.Text="平均分为:"+dr[0].ToString();
}
else if(Ddl_content.SelectedItem.Text=="最高分")
{
Lbl_high.Visible=true;
Lbl_high.Text="最高分为:"+dr[1].ToString();
}
else if(Ddl_content.SelectedItem.Text=="总人数")
{
Lbl_all.Visible=true;
Lbl_all.Text="总人数为:"+dr[2].ToString();
}
else if(Ddl_content.SelectedItem.Text=="优秀人数")
{
Lbl_a.Visible=true;
Lbl_a.Text="优秀人数为:"+dr[2].ToString();
}
else if(Ddl_content.SelectedItem.Text=="不及格人数")
{
Lbl_unpass.Visible=true;
Lbl_unpass.Text="不及格人数为:"+dr[2].ToString();
}
else
{Lbl_note.Text="无此信息";
}
cn.Close();
}
}
}
}
8.8学生选课管理页面
学生选课管理页面窗体所示,学生选课后需要管理员进一步确认管理,来确定最终选课信息。
主要相关代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
namespace sms
{
/// <summary>
/// student_course 的摘要说明。
/// </summary>
public class student_course : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Btn_student;
protected System.Web.UI.WebControls.DataGrid Dgd_sort;
protected System.Web.UI.WebControls.Label Lbl_all;
protected System.Web.UI.WebControls.TextBox Tbx_year;
protected System.Web.UI.WebControls.Label Lbl_note;
protected System.Web.UI.WebControls.Button Btn_exit;
protected System.Web.UI.WebControls.DropDownList Ddl_course;
protected System.Web.UI.WebControls.DropDownList Ddl_teacher;
SqlConnection cn;
private void Page_Load(object sender, System.EventArgs e)
{ //教师名称下拉列表框绑定
string strconn= ConfigurationSettings.AppSettings["dsn"];
//连接本地计算机的sms数据库
SqlConnection cn0= new SqlConnection (strconn);
if(!IsPostBack)
{
cn0.Open ();
string mysql="select * from teacher";
SqlCommand cm0=new SqlCommand (mysql,cn0);
SqlDataReader dr0=cm0.ExecuteReader ();
while(dr0.Read ())
{
Ddl_teacher.Items .Add (new ListItem(dr0["Teacher_name"].ToString(),dr0["Teacher_id"].ToString()) );
}
cn0.Close ();
//课程名称下拉列表框绑定
cn0.Open ();
string mysql1="select * from course";
SqlCommand cm1=new SqlCommand (mysql1,cn0);
SqlDataReader dr1=cm1.ExecuteReader ();
while(dr1.Read ())
{
Ddl_course.Items .Add (new ListItem(dr1["Course_name"].ToString() ,dr1["Course_id"].ToString()) );
}
cn0.Close ();
}
// 在此处放置用户代码以初始化页面
cn=new SqlConnection(strconn);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Btn_student.Click += new System.EventHandler(this.Btn_student_Click);
this.Btn_exit.Click += new System.EventHandler(this.Btn_exit_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Btn_student_Click(object sender, System.EventArgs e)
{ Bindgrid();
}
public void DataGrid_delete(object sender,DataGridCommandEventArgs e)
{
string strsql="delete from student_course where ID=@id";
SqlCommand cm=new SqlCommand(strsql,cn);
cm.Parameters.Add(new SqlParameter("@ID",SqlDbType.BigInt,8));
cm.Parameters["@id"].Value=Dgd_sort.DataKeys[(int)e.Item.ItemIndex];
cm.Connection.Open();
try
{
cm.ExecuteNonQuery();
Lbl_note.Text="删除成功";
}
catch(SqlException)
{
Lbl_note.Text="删除失败";
Lbl_note.Style["color"]="red";
}
cm.Connection.Close();
Bindgrid();
}
public void Bindgrid()
{
string strsql="select student_course.ID,student.Student_id,student.Student_classid from student_course,student,teacher_course where student_course.Course_id='"+Ddl_course.SelectedItem.Value+"' and student_course.Course_year=teacher_course.Course_year and student_course.Course_year='"+Tbx_year.Text+"'and teacher_course.Teacher_id='"+Ddl_teacher.SelectedItem.Value+"'and student_course.Course_id=teacher_course.Course_id and student.Student_id=student_course.Student_id";
SqlDataAdapter da=new SqlDataAdapter(strsql,cn);
DataSet ds=new DataSet();
da.Fill(ds);
Dgd_sort.DataSource=ds;
Dgd_sort.DataBind();
string strsq="select COUNT(*) from student_course,student,teacher_course where student_course.Course_id='"+Ddl_course.SelectedItem.Value+"' and student_course.Course_year=teacher_course.Course_year and student_course.Course_year='"+Tbx_year.Text+"'and teacher_course.Teacher_id='"+Ddl_teacher.SelectedItem.Value+"'and student_course.Course_id=teacher_course.Course_id and student.Student_id=student_course.Student_id";
SqlCommand cm1=new SqlCommand(strsq,cn);
cn.Open();
SqlDataReader dr=cm1.ExecuteReader();
if(dr.Read())
{
Lbl_all.Text="选课总人数为:"+dr[0].ToString();
}
else
{
Lbl_all.Text="无人选此课";
}
cn.Close();
}
private void Btn_exit_Click(object sender, System.EventArgs e)
{
Response.Redirect("default.aspx");
}
}
}
8.9学生选课页面
学生可进入学生选课页面浏览本学期待选课程的信息,可以设定查询条件,可以根据选此课的人数,教师等情况来确定是否选择此课。
主要相关代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
namespace sms
{
/// <summary>
/// sortcourse 的摘要说明。
/// </summary>
public class sortcourse : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid Dgd_sort;
protected System.Web.UI.WebControls.TextBox Tbx_courseid;
protected System.Web.UI.WebControls.TextBox Tbx_year;
protected System.Web.UI.WebControls.Button Btn_look;
protected System.Web.UI.WebControls.TextBox Tbx_teacher;
protected System.Web.UI.WebControls.TextBox Tbx_term;
protected System.Web.UI.WebControls.Button Btn_sort;
protected System.Web.UI.WebControls.Label Lbl_all;
protected System.Web.UI.WebControls.Label Lbl_note;
protected System.Web.UI.WebControls.Button Btn_yes;
protected System.Web.UI.WebControls.Button Btn_no;
protected System.Web.UI.WebControls.Panel Pn_yes;
protected System.Web.UI.WebControls.CustomValidator Cv_course;
protected System.Web.UI.WebControls.RequiredFieldValidator Rfv_year;
SqlConnection cn;
protected System.Web.UI.WebControls.DropDownList Ddl_course;string strsql;
private void Page_Load(object sender, System.EventArgs e)
{ string strconn= ConfigurationSettings.AppSettings["dsn"];
cn=new SqlConnection(strconn);
if(!IsPostBack)
{
//课程名称下拉列表框绑定
SqlConnection cn0=new SqlConnection(strconn);
cn0.Open ();
string mysql1="select * from course where Course_kind='2'";
SqlCommand cm1=new SqlCommand (mysql1,cn0);
SqlDataReader dr1=cm1.ExecuteReader ();
Ddl_course.Items .Add (new ListItem("","") );
while(dr1.Read ())
{
Ddl_course.Items .Add (new ListItem(dr1["Course_name"].ToString() ,dr1["Course_id"].ToString()) );
}
cn0.Close ();
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
} /// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Btn_sort.Click += new System.EventHandler(this.Btn_sort_Click);
this.Cv_course.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.Cv_course_ServerValidate);
this.Btn_yes.Click += new System.EventHandler(this.Btn_yes_Click);
this.Btn_no.Click += new System.EventHandler(this.Btn_no_Click);
this.Btn_look.Click += new System.EventHandler(this.Btn_look_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Btn_sort_Click(object sender, System.EventArgs e)
{
string strsq="select COUNT(Student_id) from student_course where Course_id='"+Tbx_courseid.Text+"' and Course_year='"+Tbx_year.Text+"'";
SqlCommand cm1=new SqlCommand(strsq,cn);
cn.Open();
SqlDataReader dr=cm1.ExecuteReader();
if(dr.Read())
{
Lbl_all.Text="选此课总人数为:"+dr[0].ToString();
}
else
{
Lbl_all.Text="无人选此课";
}
Pn_yes.Visible=true;
cn.Close();
}
private void Btn_yes_Click(object sender, System.EventArgs e)
{
strsql="insert into student_course(Student_id,Course_id,Course_year) values(@Student_id,@Course_id,@Course_year)";
SqlCommand cm=new SqlCommand(strsql,cn);
cm.Parameters.Add(new SqlParameter("@Student_id",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Course_id",SqlDbType.VarChar,50));
cm.Parameters.Add(new SqlParameter("@Course_year",SqlDbType.VarChar,50));
cm.Parameters["@Student_id"].Value=Session["User_id"].ToString();
cm.Parameters["@Course_id"].Value=Tbx_courseid.Text;
cm.Parameters["@Course_year"].Value=Tbx_year.Text;
cn.Open();
try
{
cm.ExecuteNonQuery();
}
catch(SqlException)
{
Lbl_note.Text="添加失败!";
}
cn.Close();
}
private void Btn_no_Click(object sender, System.EventArgs e)
{
Lbl_all.Text="请重新选择";
}
private void Cv_course_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
cn.Open();
strsql="select * from course where Course_id='"+Tbx_courseid.Text+"'";
SqlCommand cm=new SqlCommand(strsql,cn);
SqlDataReader dr=cm.ExecuteReader();
if(dr.Read())
{
args.IsValid=true;
}
else
{
args.IsValid=false;
}
cn.Close();
}
private void Btn_look_Click(object sender, System.EventArgs e)
{
if(Ddl_course.SelectedItem.Text=="")
{
if(Tbx_teacher.Text=="")
{strsql="select course.Course_name,course.Course_id,teacher.Teacher_name,course.Course_credit,course.Course_period from course,teacher,teacher_course where teacher_course.Course_year='"+Tbx_term.Text+"' and teacher_course.Course_id=course.Course_id and teacher_course.Teacher_id=teacher.Teacher_id";
}
else
{
strsql="select course.Course_name,course.Course_id,teacher.Teacher_name,course.Course_credit,course.Course_period from course,teacher,teacher_course where teacher_course.Course_year='"+Tbx_term.Text+"' and teacher_course.Course_id=course.Course_id and teacher_course.Teacher_id=teacher.Teacher_id and teacher.Teacher_name='"+Tbx_teacher.Text+"'";
}
}
else
{
if(Tbx_teacher.Text=="")
{
strsql="select course.Course_name,course.Course_id,teacher.Teacher_name,course.Course_credit,course.Course_period from course,teacher,teacher_course where teacher_course.Course_year='"+Tbx_term.Text+"' and teacher_course.Course_id=course.Course_id and teacher_course.Teacher_id=teacher.Teacher_id and course.Course_name='"+Ddl_course.SelectedItem.Text+"'";
}
else
{
strsql="select course.Course_name,course.Course_id,teacher.Teacher_name,course.Course_credit,course.Course_period from course,teacher,teacher_course where teacher_course.Course_year='"+Tbx_term.Text+"' and teacher_course.Course_id=course.Course_id and teacher_course.Teacher_id=teacher.Teacher_id and course.Course_name='"+Ddl_course.SelectedItem.Text+"' and teacher.Teacher_name='"+Tbx_teacher.Text+"'";
}
}
SqlDataAdapter da=new SqlDataAdapter(strsql,cn);
DataSet ds=new DataSet();
da.Fill(ds);
Dgd_sort.DataSource=ds;
Dgd_sort.DataBind();
}
}
}