基于面向对象的程序开发思想,结合C#窗体应用程序开发步骤,采用ADO.NET访问数据库相应组件,构建完成一套学生成绩系统。学生成绩系统是学校教务系统中不可缺少的一个子系统,它涉及到学生、课程等信息的结合。本套学生成绩系统主要包括以下四个功能模块,具体功能要求如下:
1、登录模块
登录模块提供用户登录界面,用户输入正确的用户名和密码后,则可进入系统主窗口(即导航页面),从而可以选择进入相应的子系统。
2、学生信息模块
学生信息模块主要用户管理学生的基本信息,包括学号、姓名、性别、年龄和所在系,能对学生信息进行添加、删除和修改等操作。
3、课程信息模块
课程信息模块主要用于管理课程信息,包括课程号、课程名、学分和学时,能对课程信息进行添加、删除和修改等操作。
4、成绩信息模块
成绩信息模块主要用于管理学生成绩的录入及查询
代码比较多,直接从第二个选课系统写起
public frmCourse()
{
InitializeComponent();
}
void objOpen()
{
txtNo.Enabled = true;
txtName.Enabled = true;
txtCredit.Enabled = true;
txtPeriod.Enabled = true;
cmbSemester.Enabled = true;
txtNo.Focus();
}
void objClose()
{
txtNo.Enabled = false;
txtName.Enabled = false;
txtCredit.Enabled = false;
txtPeriod.Enabled = false;
cmbSemester.Enabled = false;
}
void Clear()
{
txtNo.Text = "";
txtName.Text = "";
txtCredit.Text = " ";
txtPeriod.Text = "";
}
void RefreshData()
{
string comStr;
DataBase db = new DataBase();
DataSet ds = new DataSet();
comStr = "select * from tbl_Course";
ds = db.GetDataFromDB(comStr);
if (ds == null)
{
MessageBox.Show("没有任何学生记录!");
}
else
{
dgrdvCourse.DataSource = ds.Tables[0];
SetHeaderText();
}
}
void SetHeaderText()
{
dgrdvCourse.Columns[0].HeaderText = "课程号";
dgrdvCourse.Columns[1].HeaderText = "课程名";
dgrdvCourse.Columns[2].HeaderText = "学分";
dgrdvCourse.Columns[3].HeaderText = "学期";
dgrdvCourse.Columns[4].HeaderText = "学时";
}
private void frmCourse_Load(object sender, EventArgs e)
{
objClose();
RefreshData();
}
private void dgrdvCourse_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
int n = this.dgrdvCourse.CurrentCell.RowIndex;
txtNo.Text = this.dgrdvCourse[0, n].Value.ToString();
txtName.Text = this.dgrdvCourse[1, n].Value.ToString();
cmbSemester.SelectedItem = this.dgrdvCourse[2, n].Value.ToString();
txtCredit.Text = this.dgrdvCourse[3, n].Value.ToString();
txtPeriod.Text = this.dgrdvCourse[4, n].Value.ToString();
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
if (btnAdd.Text.Trim() == "添加")
{
btnAdd.Text = "确定";
objOpen();
Clear();
btnUpdate.Enabled = false;
btnDelete.Enabled = false;
btnClose.Enabled = false;
dgrdvCourse.Enabled = false;
}
else
{
btnAdd.Text = "添加";
if (txtNo.Text.Trim() != "" && txtName.Text.Trim() != "")
{
string sqlStr;
sqlStr = "insert into tbl_Course values ('" + txtNo.Text.Trim() + "','" + txtName.Text.Trim() + "','" + txtCredit.Text.Trim() + "','" + cmbSemester.Text.Trim() + "','" + txtPeriod.Text.Trim() + "')";
DataBase db = new DataBase();
bool b;
b = db.UpdateDB(sqlStr);
if (b == true)
{
if (MessageBox.Show("添加成功!继续添加吗?", "添加", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
Clear();
objOpen();
btnAdd.Text = "确定";
}
else
{
objClose();
btnClose.Enabled = true;
btnDelete.Enabled = true;
btnUpdate.Enabled = true;
dgrdvCourse.Enabled = true;
}
}
else
{
return;
}
}
else
{
MessageBox.Show("学号与姓名不能为空!");
txtNo.Focus();
btnAdd.Text = "确定";
}
RefreshData();
txtNo.SelectAll();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Clear();
objClose();
dgrdvCourse.Enabled = false;
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
if (btnUpdate.Text.Trim() == "修改")
{
btnUpdate.Text = "确定";
btnAdd.Enabled = false;
btnDelete.Enabled = false;
btnClose.Enabled = false;
txtName.Enabled = true;
txtAge.Enabled = true;
cmbSex.Enabled = true;
txtDept.Enabled = true;
txtName.Focus();
}
else
{
btnUpdate.Text = "修改";
btnAdd.Enabled = true;
btnDelete.Enabled = true;
btnClose.Enabled = true;
objClose();
string sqlStr;
sqlStr = "Update tbl_Student set Sname='" + txtName.Text.Trim() + "',Ssex='" + cmbSex.Text.Trim() + "', Sage = " + txtAge.Text.Trim() + ", Sdept = '" + txtDept.Text.Trim() + "'where Sno = '" + txtNo.Text.Trim() + "'";
DataBase db = new DataBase();
db.UpdateDB(sqlStr);
RefreshData();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
if (txtNo.Text.Trim() != "")
{
if (MessageBox.Show("确定要删除课程吗?", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
string sqlStr;
sqlStr = "delete from tbl_Course where Cno='" + txtNo.Text.Trim() + "'";
DataBase db = new DataBase();
db.UpdateDB(sqlStr);
RefreshData();
}
}
else
{
MessageBox.Show("没有可删除的记录!", "提示");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Hide();
}
private void btnGradeIn_Click(object sender, EventArgs e)
{
if (cmbStudent.Text == "" || cmbCourse.Text == "" ||txtGrade.Text == "" )
{
MessageBox.Show("插入信息不完整!");
}
else
{
try
{
string strcon = "data source=localhost;uid=sa;pwd=pjl; database = SelectCourse";
SqlConnection myconn = new SqlConnection(strcon);
myconn.Open();
string mysql = "insert into tbl_SC values ('" + cmbStudent.Text.Trim().Substring(0, 4) + "','" + cmbCourse.Text.Trim().Substring(0, 4)+ "','" + txtGrade.Text.Trim() + "')";
MessageBox.Show("导入成功");
string sqlStr = string.Format("SELECT tbl_SC.Sno,tbl_Student.Sname,tbl_SC.Cno,tbl_Course.Cname,tbl_SC.grade FROM tbl_Student,tbl_Course,tbl_SC where tbl_Course.Cno=tbl_SC.Cno and tbl_Student.Sno=tbl_SC.Sno ", this.txtSelText.Text.Trim());
SqlCommand mycommand = myconn.CreateCommand();
mycommand.CommandText = mysql;
mycommand.ExecuteNonQuery();
btnSelAll_Click(this, e);
myconn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void btnSelAll_Click(object sender, EventArgs e)
{
string sqlStr = string.Format("SELECT tbl_SC.Sno,tbl_Student.Sname,tbl_SC.Cno,tbl_Course.Cname,tbl_SC.grade FROM tbl_Student,tbl_Course,tbl_SC where tbl_Course.Cno=tbl_SC.Cno and tbl_Student.Sno=tbl_SC.Sno ", this.txtSelText.Text.Trim());
//string sqlStr = string.Format("SELECT tbl_SC.Sno,tbl_Student.Sname,tbl_SC.Cno,tbl_Course.Cname,tbl_SC.grade FROM tbl_Student,tbl_Course,tbl_SC where tbl_Course.Cno=tbl_SC.Cno and tbl_Student.Sno=tbl_SC.Sno and {0}='{1}'", selectcondition, this.txtSelText.Text.Trim());
DataBase db = new DataBase();
DataSet dataSetSelect = new DataSet();
dataSetSelect = db.GetDataFromDB(sqlStr);
if (dataSetSelect.Tables[0].Rows.Count == 0)
{
MessageBox.Show("没有符合条件的成绩记录!");
}
else
{
dgrdvResult.DataSource = dataSetSelect.Tables[0];
SetHeaderText();
}
}