大学数据库课程设计–一个简单的学生选课系统

一、系统简介

一个超级简单的学生选课系统,使用Windows窗体设计界面,使用C#语言实现各种功能,数据库使用的是SQL。由于时间原因,做的非常仓促,系统中没有查询功能,能够实现数据的增删改。下面开始系统演示吧。

二、系统演示

1、登录界面

在这里插入图片描述

我使用Windows窗体设计成这个样子 看着还算不错,嘻嘻。

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if(pictureBox1.Location.X<150)
            {
                pictureBox1.Location = new Point(pictureBox1.Location.X + 1, pictureBox1.Location.Y);
            }//图标移动
            else
            {
                if(comboBox1.Text == "学生")
                {
                    string sql = "select * from Stu where Name = '" + textBox1.Text + "'and Password ='" + textBox2.Text + "'";
                    Dao dao = new Dao();
                    IDataReader dr = dao.read(sql);
                    dr.Read();
                    string Sid = dr["ID"].ToString();
                    Form3 form3 = new Form3(Sid);
                    form3.Show();//显示这个窗体
                    this.Hide();//隐藏这个窗体
                }
                else
                {
                    if(comboBox1.Text == "老师")
                    {
                        string sql = "select * from Teacher where Name = '" + textBox1.Text + "'and Password ='" + textBox2.Text + "'";
                        Dao dao = new Dao();
                        IDataReader dr = dao.read(sql);
                        dr.Read();
                        string Tid = dr["ID"].ToString();
                        Form7 form7 = new Form7(Tid);
                        form7.Show();
                        this.Hide();
                    }
                    else
                    {
                        if(comboBox1.Text == "管理员")
                        {
                            Form5 form5 = new Form5();
                            form5.Show();
                            this.Hide();
                        }
                    }
                }
            
                timer1.Stop();
            }
        }
    
       
        //判断是否登录
        private string login()
        {
            if(textBox1.Text == ""||textBox2.Text == ""||comboBox1.Text == "")
            {
                MessageBox.Show("输入不完整,请检查","提示",MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            if(comboBox1.Text == "学生")
            {
                string sql = "select * from Stu where Name = '" +textBox1.Text+ "'and Password ='" +textBox2.Text+ "'";
                Dao dao = new Dao();

                IDataReader dr = dao.read(sql);
                if(dr.Read())
                {
                    return "学生";
                }
                else
                {
                    MessageBox.Show("用户名或密码错误");
                    return "-1";
                }


            }
            if(comboBox1.Text == "老师")
            {
                string sql = "select * from Teacher where Name = '" +textBox1.Text+ "'and Password ='" +textBox2.Text+ "'";
                Dao dao = new Dao();

                IDataReader dr = dao.read(sql);
                if (dr.Read())
                {
                    return "老师";
                }
                else
                {
                    MessageBox.Show("用户名或密码错误");
                    return "-1";
                }
            }
            if(comboBox1.Text == "管理员")
            {
                if(textBox1.Text == "admin"&&textBox2.Text == "admin")
                {
                    return "管理员";
                }
                else
                {
                    MessageBox.Show("用户名或密码错误");
                    return "-1";
                }
            }

            return "-1";
        }
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = null;
            textBox2.Text = null;
            comboBox1.Text = null;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        //登录事件
        private void button1_Click_1(object sender, EventArgs e)
        {
            if (login() != "-1")
            {
                timer1.Start();//启动计时器控件,图片开始移动
                textBox1.Visible = false;
                textBox2.Visible = false;
                comboBox1.Visible = false;
                label1.Visible = false;
                label2.Visible = false;
                label3.Visible = false;
            }
            else
            {
                MessageBox.Show("信息错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();//强行结束整个程序
        }
    }

上述为系统登录的代码,请结合一些注释理解。系统登录界面,有三个权限,分别为学生、老师及管理员,在代码中,都有对应的代码 ,请仔细看。

2、学生操作界面

在这里插入图片描述

 public partial class Form3 : Form
    {
        string SID;
        public Form3(string Sid)
        {
            SID = Sid;
            InitializeComponent();
            toolStripStatusLabel3.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//显示当前时间
            toolStripStatusLabel1.Text = "欢迎学号为" + SID + "的同学登录选课系统";
            timer1.Start();
            Table();
        }
        public void Table()
        {
            dataGridView1.Rows.Clear();
            string sql = "select * from Course";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b, c, d;
                a = dr["ID"].ToString();
                b = dr["Name"].ToString();
                c = dr["Credit"].ToString();
                d = dr["Teacher"].ToString();
             
                string[] str = { a, b, c, d};
                dataGridView1.Rows.Add(str);
            }
            dr.Close();//关闭链接
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel3.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//显示当前时间
        }

        private void Form3_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();//强行结束整个程序
        }

        private void 选课ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string Cid = dataGridView1.SelectedCells[0].Value.ToString();//获取选中得课程号
            string sql1 = "Select * from  CourseChoose where Sid = '"+SID+"' and Cid = '"+Cid+"'";
           
            Dao dao = new Dao();
            IDataReader dc = dao.read(sql1);

            if(!dc.Read())
            {               
                string sql = "Insert into CourseChoose values('" + SID + "','" + Cid + "')";
             
                int i = dao.Execute(sql);
                if(i>0)
                {
                    MessageBox.Show("选课成功");
                }
                
            }
                else
                {
                    MessageBox.Show("不允许重复选课");
                }
        }
        private void 我的课程ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form31 f = new Form31(SID);
            f.Show();

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Show();//显示这个窗体
            this.Hide();//隐藏这个窗体
        }

        
        private void 修改个人密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form32 f = new Form32(SID);
            f.ShowDialog();
        }

         
    }

在这里插入图片描述

 
    public partial class Form31 : Form
    {   string SID;
        public Form31(string Sid)
        {
            SID = Sid;
            InitializeComponent();
            Table();
        }
        public void Table()
        {
            dataGridView1.Rows.Clear();
            string sql = "select * from CourseChoose where Sid = '"+SID+"'";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string CID = dr["Cid"].ToString();
                string sql2 = "select* from Course where ID= '" + CID + "'";
                IDataReader dr2 = dao.read(sql2);
                dr2.Read();
                string a, b, c, d;
                a = dr2["ID"].ToString();
                b = dr2["Name"].ToString();
                c = dr2["Credit"].ToString();
                d = dr2["Teacher"].ToString();

                string[] str = { a, b, c, d };
                dataGridView1.Rows.Add(str);
                dr2.Close();
            }
            dr.Close();//关闭链接
        }
        private void Form31_Load(object sender, EventArgs e)
        {

        }

        private void 取消这门课ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult r = MessageBox.Show("确定要取消吗?", "提示", MessageBoxButtons.OKCancel);
            if (r == DialogResult.OK)
            {
                string Cid = dataGridView1.SelectedCells[0].Value.ToString();
                string sql = "delete CourseChoose where Sid = '" + SID + "' and Cid ='" + Cid + "'";
                Dao dao = new Dao();
                dao.Execute(sql);
                Table();
            }
               
        }
    }

在这里插入图片描述

public partial class Form32 : Form
    {
        string SID;
        public Form32()
        {
            InitializeComponent();
        }

        public Form32(string Sid)
        {
            InitializeComponent();
            SID = Sid;
            string sql = "select * from Stu where ID = '" + SID + "'";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);

            dr.Read();
            textBox1.Text = dr["PassWord"].ToString();
            dr.Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "Update Stu set Password = '" + textBox2.Text +"'where ID = '"+SID+"'";
            Dao dao = new Dao();
            int i = dao.Execute(sql);

            if(i>0)
            {
                MessageBox.Show("修改成功");
            }

        
            this.Hide();//隐藏这个窗体
        }

        private void Form32_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();//强行结束整个程序
        }
    }

上述三个窗体为学生操作面,在第一张主操作界面,点击不同的按钮,会跳转至不同的操作界面, 窗体下给出控制代码.

3、老师操作界面

在这里插入图片描述

public partial class Form7 : Form
    {
        string TID;
        public Form7(string Tid)
        {
            TID = Tid;
            InitializeComponent();
            toolStripStatusLabel1.Text = "欢迎工号为" + TID + "的老师登录选课系统";
            Table();
        }

        public void Table()
        {
            dataGridView1.Rows.Clear();
            string sql = "select * from Teacher where ID = '"+TID+"'";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string NAME = dr["Name"].ToString();
                string sql2 = "select* from Course where Teacher= '" + NAME + "'";
                IDataReader dr2 = dao.read(sql2);
                dr2.Read();
                string a, b, c;
                a = dr2["ID"].ToString();
                b = dr2["Name"].ToString();
                c = dr2["Credit"].ToString();
              
                string[] str = { a, b, c};
                dataGridView1.Rows.Add(str);
            }
            dr.Close();//关闭链接
        }
        private void 修改个人密码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form8 f = new Form8(TID);
            f.ShowDialog();
        }

        private void 系统退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Show();//显示这个窗体
            this.Hide();//隐藏这个窗体
        }

        private void Form7_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();//强行结束整个程序
        }
    }

在这里插入图片描述

  public partial class Form8 : Form
    {
        string TID;
        public Form8()
        {
            InitializeComponent();
        }

        public Form8(string Tid)
        {
            InitializeComponent();
            TID = Tid;
            string sql = "select * from Teacher where ID = '" + TID + "'";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);

            dr.Read();
            textBox1.Text = dr["PassWord"].ToString();
            dr.Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "Update Teacher set Password = '" + textBox2.Text + "'where ID = '" + TID + "'";
            Dao dao = new Dao();
            int i = dao.Execute(sql);
            if (i > 0)
            {
                this.Hide();
                MessageBox.Show("修改成功");
            }
            this.Hide();//隐藏这个窗体
        }  
    }

上述两个窗体为老师操作界面,在第一张主操作界面,点击不同的按钮,会跳转至不同的操作界面, 窗体下给出控制代码。老师操作界面功能比较少。

4、管理员操作界面

在这里插入图片描述

 public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();//显示这个窗体
            this.Hide();

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form4 form4 = new Form4();
            form4.Show();//显示这个窗体
            this.Hide();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Form6 form6 = new Form6();
            form6.Show();//显示这个窗体
            this.Hide();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Show();//显示这个窗体
            this.Hide();
        }
    }

在这里插入图片描述

public partial class Form2 : Form
    {

        public Form2()
        {
            InitializeComponent();
            toolStripStatusLabel3.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//显示当前时间
            timer1.Start();
            Table();
        }
        private void Form2_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();//强行结束整个程序
        }
        //让表显示数据
        public void Table()
        {
            dataGridView1.Rows.Clear();
            string sql = "select * from Stu";
            Dao dao = new Dao();
            IDataReader dr = dao.read(sql);
            while (dr.Read())
            {
                string a, b, c, d, e;
                a = dr["ID"].ToString();
                b = dr["Name"].ToString();
                c = dr["Sex"].ToString();
                d = dr["Class"].ToString();
                e = dr["Dept"].ToString();
                string[] str = { a, b, c, d, e };
                dataGridView1.Rows.Add(str);
            }
            dr.Close();//关闭链接
        }

        private void 添加学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form21 f = new Form21(this);
            f.ShowDialog();//添加学生信息的窗体
        }
    
        private void 修改学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string[] str = { dataGridView1.SelectedCells[0].Value.ToString(), dataGridView1.SelectedCells[1].Value.ToString(), dataGridView1.SelectedCells[2].Value.ToString(), dataGridView1.SelectedCells[3].Value.ToString(), dataGridView1.SelectedCells[4].Value.ToString() };
       
            Form21 f = new Form21(str,this); 
            f.ShowDialog(); 
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form5 form5 = new Form5();
            form5.Show();//显示这个窗体
            this.Hide();//隐藏这个窗体
        }

        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();
                name = dataGridView1.SelectedCells[1].Value.ToString();
                string sql = "delete from Stu where ID = '" + id + "'and Name = '" + name + "'";
                 
                Dao dao = new Dao();
                dao.Execute(sql);
                dataGridView1.Rows.Clear();
                Table();
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            Table();
        }
    }

在这里插入图片描述

  

在这里插入图片描述

  public partial class Form21 : Form
    {
        Form2 form2;

        string[] str = new string[5];

        public Form21(Form2 f)
        {
            InitializeComponent();
            button5.Visible = false; //插入时 隐藏修改  
            form2 = f;
        }
            //用于修改 参数位于一个数组
        public Form21(string[] a,Form2 f) 
        {
            InitializeComponent();
            for(int i=0;i<5;i++)
            {
                str[i] = a[i];
            }
            textBox1.Text = str[0];
            textBox2.Text = str[1];
            textBox3.Text = str[2];
            textBox4.Text = str[3];
            textBox5.Text = str[4];
            button1.Visible = false;//修改时 隐藏插入
            form2 = f; 
        }
        private void Form21_Load(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }
        //添加一条学生事件
        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 Stu 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();
                this.Hide();
                  
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = null;
            textBox1.Text = null;
            textBox1.Text = null;
            textBox1.Text = null;
            textBox1.Text = null;
            textBox1.Text = null;
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "")
            {
                MessageBox.Show("修改后有空项,请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if(textBox1.Text!=str[0])
                {
                    string sql = "update Stu set ID = '"+textBox1.Text+"'where ID = '"+str[0]+"' and Name = '"+str[1]+"'";
                    Dao dao = new Dao();
                    dao.Execute(sql);
                    str[0] = textBox1.Text;
                }
                if (textBox2.Text != str[1])
                {
                    string sql = "update Stu set Name = '" + textBox2.Text + "'where ID = '" + str[0] + "' and Name = '" + str[1] + "'";
                    Dao dao = new Dao();
                    dao.Execute(sql);
                    str[1] = textBox2.Text;
                }
                if (textBox3.Text != str[2])
                {
                    string sql = "update Stu set Sex = '" + textBox3.Text + "'where ID = '" + str[0] + "' and Name = '" + str[1] + "'";
                    Dao dao = new Dao();
                    dao.Execute(sql);
                    str[2] = textBox3.Text;
                }
                if (textBox4.Text != str[3])
                {
                    string sql = "update Stu set Class= '" + textBox4.Text+ "'where ID = '" + str[0] + "' and Name = '" + str[1] + "'";
                    Dao dao = new Dao();
                    dao.Execute(sql);
                    str[3] = textBox4.Text;
                }
                if (textBox5.Text != str[4])
                {
                    string sql = "update Stu set Dept = '" + textBox5.Text+ "'where ID = '" + str[0] + "' and Name = '" + str[1] + "'";
                    Dao dao = new Dao();
                    dao.Execute(sql);
                    str[4] = textBox5.Text;
                }
                form2.Table();
                this.Hide();
            }
        }

        private void Form21_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();//强行结束整个程序
        }
    }

在这里插入图片描述

public partial class Form41 : Form   
 {        
				Form4 form4;        
				string[] str = new string[5];        
				public Form41(Form4 f)        
				{            
					InitializeComponent();            
					button3.Visible = false; //插入时 隐藏修改             
					form4 = f;        
				}        //用于修改 参数位于一个数组       
				 public Form41(string[] a,Form4 f)        
				 {            
					 InitializeComponent(); 
					 for (int i = 0; i < 5; i++)           
                     {                
			             str[i] = a[i];        
                     }           
		                  textBox1.Text = str[0];  
                          textBox2.Text = str[1];    
                          textBox3.Text = str[2];     
                          textBox4.Text = str[3];   
                          textBox5.Text = str[4];         
                          button1.Visible = false;//修改时 隐藏插入           
                          form4 = f;  
                  }        //添加一条学生信息        
                  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 Teacher 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;                
	                  	 }                
	                  		  form4.Table();                
	                  		  this.Hide();            
	                 }        
                 }         
                  		  private void button2_Click(object sender, EventArgs e)        
                  		  {            
                  		                  textBox1.Text = null;            
				                  		  textBox2.Text = null;            
				                  		  textBox3.Text = null;            
				                  		  textBox4.Text = null;           
				                  		  textBox5.Text = null;                   
                  		    }        
						private void button3_Click(object sender, EventArgs e)        
						{            
								if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "")            
								{                
										MessageBox.Show("修改后有空项,请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);            	
										}           
							   else            
							   {                
							   if (textBox1.Text != str[0])                
							   {                    
							   string sql = "update Teacher set ID = '" + textBox1.Text + "'where ID = '" + str[0] + "' and Name = '" + str[1] + "'";                   
							    Dao dao = new Dao();                   
							    dao.Execute(sql);                    
							    str[0] = textBox1.Text;               
							    }                
							    if (textBox2.Text != str[1])                
							    {                    
							    		string sql = "update Teacher set Name = '" + textBox2.Text + "'where ID = '" + str[0] + "' and Name = '" + str[1] + "'";                   
							    		 Dao dao = new Dao();                    
							    		 dao.Execute(sql);                    
							    		 str[1] = textBox2.Text;                
							    }                
							    if (textBox3.Text != str[2])                
							    {                    
							    	string sql = "update Teacher set Sex = '" + textBox3.Text + "'where ID = '" + str[0] + "' and Name = '" + str[1] + "'";                    
							    	Dao dao = new Dao();                    
							    	dao.Execute(sql);                    
							    	str[2] = textBox3.Text;               
							     }                
							     if (textBox4.Text != str[3])                
							     {                    
							     		string sql = "update Teacher set Dept= '" + textBox4.Text + "'where ID = '" + str[0] + "' and Name = '" + str[1] + "'";                   
							     	    Dao dao = new Dao();                    
							     	    dao.Execute(sql);                    
							     	    str[3] = textBox4.Text;                
							     }                
							     if (textBox5.Text != str[4])                
							     {                    
							     		string sql = "update Teacher set Zc = '" + textBox5.Text + "'where ID = '" + str[0] + "' and Name = '" + str[1] + "'";                   
							     	    Dao dao = new Dao();                    
							     	    dao.Execute(sql);                   
							     	    str[4] = textBox5.Text;               
							     }                
							     form4.Table();                
							     this.Hide();            
							     }       
		}    

}

在这里插入图片描述

 public partial class Form6 : Form    {        public Form6()        {            InitializeComponent();                         Table();        }        public void Table()        {            dataGridView1.Rows.Clear();            string sql = "select * from Course";            Dao dao = new Dao();            IDataReader dr = dao.read(sql);            while (dr.Read())            {                string a, b, c, d;                a = dr["ID"].ToString();                b = dr["Name"].ToString();                c = dr["Credit"].ToString();                d = dr["Teacher"].ToString();                string[] str = { a, b, c, d};                dataGridView1.Rows.Add(str);            }            dr.Close();//关闭链接        }        private void 添加课程ToolStripMenuItem1_Click(object sender, EventArgs e)        {            Form61 f = new Form61(this);            f.ShowDialog();//添加学生信息的窗体        }        private void 修改课程ToolStripMenuItem_Click(object sender, EventArgs e)        {            string[] str = { dataGridView1.SelectedCells[0].Value.ToString(), dataGridView1.SelectedCells[1].Value.ToString(), dataGridView1.SelectedCells[2].Value.ToString(), dataGridView1.SelectedCells[3].Value.ToString() };            Form61 f = new Form61(str, this);            f.ShowDialog();        }        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();                name = dataGridView1.SelectedCells[1].Value.ToString();                string sql = "delete from Course where ID = '" + id + "'and Name = '" + name + "'";                MessageBox.Show(sql);                Dao dao = new Dao();                dao.Execute(sql);                dataGridView1.Rows.Clear();                Table();            }        }        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)        {            Form5 form5 = new Form5();            form5.Show();//显示这个窗体            this.Hide();//隐藏这个窗体        }    }

在这里插入图片描述

public partial class Form61 : Form    {        Form6 form6;        string[] str = new string[4];        public Form61(Form6 f)        {            InitializeComponent();            button3.Visible = false; //插入时 隐藏修改             form6 = f;        }        public Form61(string[] a, Form6 f)        {            InitializeComponent();            for (int i = 0; i < 4; i++)            {                str[i] = a[i];            }            textBox1.Text = str[0];            textBox2.Text = str[1];            textBox3.Text = str[2];            textBox4.Text = str[3];                    button1.Visible = false;//修改时 隐藏插入            form6 = f ;        }        private void label1_Click(object sender, EventArgs e)        {        }        private void textBox4_TextChanged(object sender, EventArgs e)        {        }        private void button1_Click(object sender, EventArgs e)        {            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == ""  )            {                MessageBox.Show("输入不完整,请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);            }            else            {                string sql = "Insert into Course values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "' )";                MessageBox.Show(sql);                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;                                 }                form6.Table();                this.Hide();            }        }        private void button3_Click(object sender, EventArgs e)        {            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")            {                MessageBox.Show("修改后有空项,请检查", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);            }            else            {                if (textBox1.Text != str[0])                {                    string sql = "update Course set ID = '" + textBox1.Text + "'where ID = '" + str[0] + "' and Name = '" + str[1] + "'";                    Dao dao = new Dao();                    dao.Execute(sql);                    str[0] = textBox1.Text;                }                if (textBox2.Text != str[1])                {                    string sql = "update Course set Name = '" + textBox2.Text + "'where ID = '" + str[0] + "' and Name = '" + str[1] + "'";                    Dao dao = new Dao();                    dao.Execute(sql);                    str[1] = textBox2.Text;                }                if (textBox3.Text != str[2])                {                    string sql = "update Course set Credit = '" + textBox3.Text + "'where ID = '" + str[0] + "' and Name = '" + str[1] + "'";                    Dao dao = new Dao();                    dao.Execute(sql);                    str[2] = textBox3.Text;                }                if (textBox4.Text != str[3])                {                    string sql = "update Course set Teacher = '" + textBox4.Text + "'where ID = '" + str[0] + "' and Name = '" + str[1] + "'";                    Dao dao = new Dao();                    dao.Execute(sql);                    str[3] = textBox4.Text;                }                              form6.Table();                this.Hide();            }        }        private void button2_Click(object sender, EventArgs e)        {            textBox1.Text = null;            textBox2.Text = null;            textBox3.Text = null;            textBox4.Text = null;                    }  }

上述窗体为管理员操作界面,在第一张主操作界面,点击不同的按钮,会跳转至不同的操作界面, 窗体下给出控制代码.管理员权限的用户名及密码皆为admin

4、数据库引用

这是数据库的课程设计 ,怎么能少了数据库尼!!!以下为数据库的链接代码。很重要!!!!

 class Dao    
 {        
		 public SqlConnection connection()        
		 {            
		 		string str = "Data Source=LAPTOP-APLE92MU;Initial Catalog=Demo;Integrated Security=True";         
		    	SqlConnection sc = new SqlConnection(str);           
		     	sc.Open();//打开数据库链接           
		        return sc;       
		}       
        public SqlCommand command(string sql)        
        {           
         	SqlCommand cmd = new SqlCommand(sql, connection());       
            return cmd;        
        }        //用于delete update insert 返回受影响的行数       
        public int Execute(string sql)       
        {            
              return command(sql).ExecuteNonQuery();       
        }        //用于select 返回SqlDataReader对象,包含select到的数据        
        public SqlDataReader read(string sql)       
       {            
               return command(sql).ExecuteReader();    
       }   
  }

这是自己写的一个Dao类自定义函数用于数据库的链接及数据库信息的更新,Data Source 是你数据库服务器名称,Catalog是你数据库的名字,后面的不变即可。

三、系统总结

这个为一个简单的学生选课系统,写的很匆忙,还有一些功能没有实现,缺少一个查询功能,还有一些BUG,此系统供大家参考,如若想要此系统源码附带样本数据库及操作指南,请点击下面链接下载 ,拒绝白嫖,从我做起。

链接: link.

Logo

华为云1024程序员节送福利,参与活动赢单人4000元礼包,更有热门技术干货免费学习

更多推荐