【Windows Form 实战】学生成绩管理系统(八) 管理员模块设计2

来源:互联网 发布:盗版玩家 知乎 编辑:程序博客网 时间:2024/05/16 08:27

1、增加课程

(1) 界面

(2) 代码

using System;using System.Data.SqlClient;using System.Windows.Forms;namespace SSMS{    public partial class frmAddCourse : Form    {        public frmAddCourse()        {            InitializeComponent();        }        private void frmAddCourse_Load(object sender, EventArgs e)        {            tbTnum.Text = "NULL";            tbTnum.ReadOnly = true;        }        private void btnAdd_Click(object sender, EventArgs e)        {            string sqlStr = @"insert into {0} values('{1}','{2}',NULL )";            sqlStr = string.Format(sqlStr, Data.t_course, tbCnum.Text, tbCname.Text);            SqlConnection con = new SqlConnection(Data.connectionString);            con.Open();            SqlCommand cmd = new SqlCommand(sqlStr, con);            cmd.ExecuteNonQuery();            con.Close();            MessageBox.Show("添加成功!");        }    }}

2、增加学生

(1) 界面

(2) 代码

using System;using System.Data;using System.Data.SqlClient;using System.Windows.Forms;namespace SSMS{    public partial class frmAddStudent : Form    {        SqlCommand cmd;        SqlConnection con;        string sqlStr;        public frmAddStudent()        {            InitializeComponent();            con = new SqlConnection(Data.connectionString);            string[] s = new string[100];            s = Data.getAllClass();            for (int i = 0; i < Data.getClassCount(); ++i)            {                cbClass.Items.Add(s[i]);            }            cbClass.SelectedIndex = 0;            cbClass.DropDownStyle = ComboBoxStyle.DropDownList;        }        private void btnAdd_Click(object sender, EventArgs e)        {            try            {                SqlConnection con = new SqlConnection(Data.connectionString);                con.Open();                sqlStr = @"insert into {0} values ('{1}', '{2}', '{3}')";                sqlStr = string.Format(sqlStr, Data.t_student, tbSnum.Text, tbSname.Text, cbClass.SelectedItem.ToString());                cmd = new SqlCommand(sqlStr, con);                cmd.ExecuteNonQuery();                MessageBox.Show("添加成功!");            }            catch (SqlException ex)            {                MessageBox.Show("添加失败! " + ex.Message);            }        }        private void frmAddStudent_FormClosing(object sender, FormClosingEventArgs e)        {            if (con.State != ConnectionState.Closed)            {                con.Close();            }        }    }}

3、增加教师

(1) 界面

(2) 代码

using System;using System.Data;using System.Data.SqlClient;using System.Windows.Forms;namespace SSMS{    public partial class frmAddTea : Form    {        SqlDataAdapter sda;        DataTable dt;        string[] s;        public frmAddTea(string tnum="", string tname="", string cname ="", string cnum = "")        {            if (tnum != "")            {                tbTnum.Text = tnum;                tbTnum.ReadOnly = true;                tbTname.Text = tname;                cbCoureName.Items.Add(cname);            }            InitializeComponent();        }        private void btnAdd_Click(object sender, EventArgs e)        {            SqlCommand cmd = new SqlCommand();            SqlConnection con = new SqlConnection(Data.connectionString);            con.Open();            string sqlStrAddTea = @"insert into {0} values ('{1}', '{2}', '{3}', '{4}')";            sqlStrAddTea = string.Format(sqlStrAddTea, Data.t_teacher, tbTnum.Text, tbTname.Text, cbCoureName.SelectedItem.ToString(), tbCourseNum.Text);            cmd.CommandText = sqlStrAddTea;            cmd.Connection = con;            cmd.ExecuteNonQuery();            string sqlStrUpdateCourse = @"update {0} set {1}='{2}' where {3}='{4}'";            sqlStrUpdateCourse = string.Format(sqlStrUpdateCourse, Data.t_course, Data.c_course_tnum, tbTnum.Text, Data.c_course_cnum, tbCourseNum.Text);            cmd.CommandText = sqlStrUpdateCourse;            cmd.ExecuteNonQuery();            con.Close();            if (tbTnum.ReadOnly == false)            {                MessageBox.Show("添加成功!");            }            else            {                MessageBox.Show("修改成功!");            }        }        private void frmAddTea_Load(object sender, EventArgs e)        {            string sqlStr = @"select {0},{1} from {2} where {3} is null";            sqlStr = string.Format(sqlStr, Data.c_course_cnum, Data.c_course_cname, Data.t_course, Data.c_course_tnum);            sda = new SqlDataAdapter(sqlStr,Data.connectionString);            dt = new DataTable();            sda.Fill(dt);            if (dt.Rows.Count <= 0)            {                lbWarning.Text = "没有可以分配的课程,请先添加课程";                btnAdd.Enabled = false;                return;            }            lbWarning.Visible = false;            s = new string[dt.Rows.Count + 1];            for (int i = 0; i < dt.Rows.Count; ++i)            {                cbCoureName.Items.Add(dt.Rows[i][1].ToString());                s[i] = dt.Rows[i][0].ToString();            }            cbCoureName.DropDownStyle = ComboBoxStyle.DropDownList;            cbCoureName.SelectedIndex = 0;            tbCourseNum.Text = s[0];            tbCourseNum.ReadOnly = true;        }        private void cbCoureName_SelectedIndexChanged(object sender, EventArgs e)        {            tbCourseNum.Text = s[cbCoureName.SelectedIndex];        }    }}

4、修改课程

(1) 界面

(2) 代码

using System;using System.Data.SqlClient;using System.Windows.Forms;namespace SSMS{    public partial class frmModiCour : Form    {        public frmModiCour(string Cnum, string Cname, string Tnum="")        {            InitializeComponent();            tbCnum.Text = Cnum;            tbCname.Text = Cname;            tbTnum.Text = Tnum;            tbCnum.ReadOnly = true;            tbTnum.ReadOnly = true;        }        private void btnModi_Click(object sender, EventArgs e)        {            string sqlStr = @"update {0} set {1}='{2}' where {3}='{4}'";            sqlStr = string.Format(sqlStr, Data.t_course, Data.c_course_cname, tbCname.Text, Data.c_course_cnum, tbCnum.Text);            SqlConnection con = new SqlConnection(Data.connectionString);            con.Open();            SqlCommand cmd = new SqlCommand(sqlStr, con);            cmd.ExecuteNonQuery();            con.Close();            MessageBox.Show("修改成功!");        }    }}

5、修改学生

(1) 界面

(2) 代码

using System;using System.Data.SqlClient;using System.Windows.Forms;namespace SSMS{    public partial class frmModiStudent : Form    {        string snum, sname, sclass;        SqlConnection con;        public frmModiStudent(string sNum, string sName = "", string sClass = "")        {            snum = sNum;            sname = sName;            sclass = sClass;            InitializeComponent();            tbSnum.Text = snum;            tbSnum.ReadOnly = true;            tbSname.Text = sname;            cbClass.Items.Add(sclass);            cbClass.DropDownStyle = ComboBoxStyle.DropDownList;            con = new SqlConnection(Data.connectionString);            string[] s = new string[100];            s = Data.getAllClass();            for (int i = 0; i < Data.getClassCount(); ++i)            {                cbClass.Items.Add(s[i]);            }            cbClass.SelectedIndex = 0;        }        private void btnModiStudent_Click(object sender, EventArgs e)        {            string sqlStrUpdate = @"update {0} set {1} = '{2}', {3} = '{4}' where {5} = '{6}';";            sqlStrUpdate = string.Format(sqlStrUpdate, Data.t_student, Data.c_student_sname, tbSname.Text, Data.c_student_sclass, cbClass.SelectedItem.ToString(), Data.c_student_snum, tbSnum.Text);            try            {                con.Open();                SqlCommand cmd = new SqlCommand(sqlStrUpdate, con);                cmd.ExecuteNonQuery();                MessageBox.Show("修改成功!");                con.Close();            }            catch (SqlException)            {                MessageBox.Show("修改失败!");            }        }    }}

6、注册开关

(1) 界面

(2) 代码

using System;using System.Data;using System.Data.SqlClient;using System.Windows.Forms;namespace SSMS{    public partial class frmEnroll : Form    {        SqlDataAdapter sda;        DataTable dt;        string sqlStr;        bool enrollOpen = false;        public frmEnroll()        {            InitializeComponent();        }        private void btn_Click(object sender, EventArgs e)        {            if (MessageBox.Show("确定更改吗?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)            {                SqlCommandBuilder scb = new SqlCommandBuilder(sda);                dt.Rows[0][0] = !enrollOpen;                sda.Update(dt);                MessageBox.Show("修改成功!");                this.Close();            }            else            {                this.Close();            }        }        private void frmEnroll_Load(object sender, EventArgs e)        {            sqlStr = @"select * from {0}";            sqlStr = string.Format(sqlStr, Data.t_enroll);            sda = new SqlDataAdapter(sqlStr, Data.connectionString);            dt = new DataTable();            sda.Fill(dt);            if (dt.Rows[0][0].ToString().Equals("True"))            {                enrollOpen = true;                rbOn.Checked = true;                rbOff.Checked = false;                rbOn.Enabled = false;                lb.Text = "当前状态是: 选课开放";            }            else            {                enrollOpen = false;                rbOff.Checked = true;                rbOn.Checked = false;                rbOff.Enabled = false;                lb.Text = "当前状态是: 选课关闭";            }            btn.Enabled = false;        }        private void rbOn_CheckedChanged(object sender, EventArgs e)        {            if (rbOn.Checked)            {                btn.Enabled = true;                lb.Text = "当前状态是: 选课开放";            }        }        private void rbOff_CheckedChanged(object sender, EventArgs e)        {            if (rbOff.Checked)            {                btn.Enabled = true;                lb.Text = "当前状态是: 选课关闭";            }        }    }}

源代码文件下载地址

0 0
原创粉丝点击