2021-03-10 学生选课管理系统 C#

学生选课管理系统 C#

实验内容

(1)设计一个学生成绩管理信息系统,能对学生的成绩记录进行方便的输入、查询、修改等操作,以及综合统计查询等。
(2)对学生成绩的综合查询,如查询个人的单科或所有成绩/不及格成绩,查询某门课程的所有程序等。
(3)至少实现对一个数据表的完整操作(CRUD,增删查改)、综合(复合条件)查询;

任务分析

(1)完成界面设计和各界面之间的跳转和衔接
(2)建立数据库
(3)建立数据库连接
(4)完善对数据库的操作

核心代码

public SqlConnection connection()
        {
            string str = "Data Source=DESKTOP-8NHPI7F\\SQLEXPRESS;Initial Catalog=MySchool;Integrated Security=True";
            SqlConnection sc = new SqlConnection(str);
            sc.Open();
            return sc;
        }
        public SqlCommand command(string sql)
        {
            SqlCommand sc = new SqlCommand(sql, connection());
            return sc;
        }
        public int Execute(string sql)//用于delete update insert 返回受影响的行数
        {
            return command(sql).ExecuteNonQuery();
        }
        public SqlDataReader read(string sql)//用于select 返回SqldataReader对象,包含select到的数据
        {
            return command(sql).ExecuteReader();
        }
if(comboBox1.Text=="学生")
            {
                string sql = "select * from Student where Id='" + textBox1.Text + "'and Password='" + textBox2.Text + "'";
                DAO dao = new DAO();
                SqlDataReader dr=dao.read(sql);
                if(dr.Read())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
public void Table()//让表显示数据
        {
            dataGridView1.Rows.Clear();
            string sql = "select * from Student";
            DAO dao = new DAO();
            SqlDataReader dr = dao.read(sql);
            while(dr.Read()) //Read()返回bool,读不读得到
            {
                string a, b, c, d, e;
                a = dr["Id"].ToString();
                b = dr["Name"].ToString();
                c = dr["Class"].ToString();
                d = dr["Birthday"].ToString();
                e = dr["JG"].ToString();
                string[] str = { a, b, c, d, e };
                dataGridView1.Rows.Add(str);//行集合 //该控件添加元素的方法

            }
              dr.Close();//读完要关掉,不然会造成资源浪费 
        }
private void 删除学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult r = MessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.OKCancel);
            if(r==DialogResult.OK)
            {
            string id, name;
            id = dataGridView1.SelectedCells[0].Value.ToString();//获取选中的那个单元格的第0个,的值,然后转换为字符串
            name= dataGridView1.SelectedCells[1].Value.ToString();
            string sql="delete from Student where Id='"+id+"'and Name='"+name+"'";
            
            DAO dao = new DAO();
            dao.Execute(sql);
            Table();
            }
    }
private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text==""|| textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" )
            {
                MessageBox.Show("输入不完整,请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
               string sql="Insert into Student values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"','"+textBox5.Text+"','123456')";
               
               DAO dao = new DAO();
                int i = dao.Execute(sql);
               if(i>0)
                {
                    MessageBox.Show("插入成功");
                    textBox1.Text = null;
                    textBox2.Text = null;
                    textBox3.Text = null;
                    textBox4.Text = null;
                    textBox5.Text = null;
                }
                form2.Table();
            }
private void 选课ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string cID = dataGridView1.SelectedCells[0].Value.ToString();//获取选中的课程号
            string sql1 = "select * from CourseRecord where sId='" + SID + "'and cId='" + cID + "'";
            DAO dao = new DAO();
            SqlDataReader dc = dao.read(sql1);
            if(!dc.Read())
            {
            string sql = "Insert into CourseRecord values('" + SID + "','" + cID + "')";
            
            int i = dao.Execute(sql);
            if(i>0)
            {
                MessageBox.Show("选课成功");
            }
            }
            else
            {
                MessageBox.Show("不允许重复选课!");
            }
           
        }

程序运行截图

登陆界面:

在这里插入图片描述
在这里插入图片描述

学生端:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

教师端:

在这里插入图片描述
在这里插入图片描述

  • 10
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值