用C#连接到数据库实现学生学籍管理系统

用C#连接到数据库实现学生学籍管理系统

前言
想必兄台是个正在实训阶段的大学生吧,如果你的实训内容是使用C#开发一个学生信息管理系统的话,那么这篇文章或许正好可以帮到你,说不定还让你的作品脱颖而出呢,话不多说,效果图和源码(需要源码的加下面的QQ群,资料都在群里),记得点赞和关注我哦!

点我进群     一起学习交流!(群里有许多的学习资料,我做过的一些网页我都上传在群里了,需要的直接下载就可以了)

QQ群:722384575

一、使用的工具
博主使用的工具是vs2010版本的,你们可以使用vs2010以上的版本,这个工具我觉得挺好用的,他还可以编写C语言代码,推荐大家使用。
二、主要功能
它可以登录、添加学生信息、修改学生信息、删除学生信息、查询学生信息等等。
三、界面设计(如需要源代码的请在评论区留言)
(1)做好准备工作,建立相关的数据库以及窗体应用文件
在这里插入图片描述
(2)页面设计效果图如下:
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.OleDb ;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 学生学籍管理系统
{
    
    
    public partial class DL : Form
    {
    
    
        public DL()
        {
    
    
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
    
    
            Application.Exit();
        }

        private void DL_Load(object sender, EventArgs e)
        {
    
    
            textBox1.Focus();
        }

        private void button1_Click(object sender, EventArgs e)
        {
    
    
            if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
            {
    
    
                MessageBox.Show("请输入用户名和密码", "错误提示");
            }
            else
            {
    
    
                DB.cn.Open();
                OleDbCommand cmd = new OleDbCommand("", DB.cn);
                string sql = "select RoleName from UserInfo where user='" + textBox1.Text.Trim() + "' and Password='" + textBox2.Text.Trim() + "'";
                cmd.CommandText = sql;
                string rolename;
                if (null != cmd.ExecuteScalar())
                {
    
    
                    rolename = cmd.ExecuteScalar().ToString();
                    this.Visible = false;
                    Main main = new Main();
                    main.Tag = this.FindForm();
                    sql = "select * from Roles where RoleName='" + rolename + "'";
                    cmd.CommandText = sql;
                    OleDbDataReader xq = cmd.ExecuteReader();
                    xq.Read();
                    main.XTMenuItem.Visible = (bool)(xq.GetValue(1));
                    main.CJMenuItem.Visible = (bool)(xq.GetValue(2));
                    main.ZYMenuItem.Visible = (bool)(xq.GetValue(3));
                    main.BJMenuItem.Visible = (bool)(xq.GetValue(4));
                    main.KCMenuItem.Visible = (bool)(xq.GetValue(5));
                    main.DAMenuItem.Visible = (bool)(xq.GetValue(6));
                    main.StatusLabel2.Text = textBox1.Text.Trim();
                    main.ShowDialog();
                    DB.cn.Close();
                }
                else
                {
    
    
                    MessageBox.Show("用户名或密码错误", "登录失败");
                    DB.cn.Close();
                }
            }
        }
    }
}

在这里插入图片描述
(3)其他的页面设计如下(部分页面)
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data ;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 学生学籍管理系统
{
    
    
    public partial class AddStuInfo : Form
    {
    
    
        public AddStuInfo()
        {
    
    
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {
    
    

        }

        private void AddStuInfo_Load(object sender, EventArgs e)
        {
    
    
            DB.cn.Close();
            DB.cn.Open();
            OleDbDataAdapter adp = new OleDbDataAdapter("select SpeName from SpecialyInfo", DB.cn);
            DataSet ds = new DataSet();
            adp.Fill(ds, "specialty");
            comboBox3.DisplayMember = "SpeName";
            comboBox3.ValueMember = "SpeName";
            comboBox3.DataSource = ds.Tables["specialty"].DefaultView;
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
    
    
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
    
    
            DB.cn.Close();
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
    
    
            if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "" || textBox3.Text.Trim() == "" || textBox4.Text.Trim() == "" || textBox5.Text.Trim() == "" || comboBox1.Text.Trim() == "" || comboBox2.Text.Trim() == "" || comboBox3.Text.Trim() == "")
            {
    
    
                MessageBox.Show("你填写的信息不完整,请继续填写完整!", "错误提示");
            }
            else
            {
    
    
                OleDbCommand cmd = new OleDbCommand("", DB.cn);
                string sql;
                sql = "select * from StudentInfo where Num='" + this.textBox1.Text.Trim() + "'";
                cmd.CommandText = sql;
                if (cmd.ExecuteScalar() != null)
                {
    
    
                    MessageBox.Show("你输入的学号已经存在,请重新输入!", "错误提示");
                }
                else
                {
    
    

                    sql = "insert into StudentInfo  values('" + textBox1.Text.Trim () + "','" + textBox2.Text.Trim () + "','" + comboBox1.Text.Trim() + "','" + textBox3.Text.Trim () + "','" + textBox4.Text.Trim () + "','" + comboBox2.Text.Trim() + "','" + textBox5.Text.Trim () + "','" + comboBox3.Text.Trim() + "')";
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("学生学籍信息注册成功!", "成功信息");
                }

            }
        }
    }
}

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 学生学籍管理系统
{
    
    
    public partial class AddClass : Form
    {
    
    
        public AddClass()
        {
    
    
            InitializeComponent();
        }

        private void button3_Click(object sender, EventArgs e)
        {
    
    
            textBox1.Text = textBox2.Text = textBox3.Text = textBox4.Text = textBox5.Text = textBox6.Text = "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
    
    
            DB.cn.Close();
            this.Close();
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
    
    
            string oldconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xsxjdb.mdb";
            OleDbConnection oleDbConnection1 = new OleDbConnection(oldconn);
            oleDbConnection1.Close();
            oleDbConnection1.Open();
            string sql;
            OleDbCommand cmd = new OleDbCommand("", oleDbConnection1);
            if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "" || textBox3.Text.Trim() == "" || textBox4.Text.Trim() == "" || textBox5.Text.Trim() == "" || textBox6.Text.Trim() == "")
            {
    
    
                MessageBox.Show("请填写全部信息!", "提示");
            }
            else
            {
    
    
                DB.cn.Close();
                DB.cn.Open();
                OleDbCommand oldcomm = new OleDbCommand("", DB.cn);
                
                sql = "select ClassNum from ClassInfo where ClassNum='" + textBox1.Text.Trim() + "'";
                oldcomm.CommandText = sql;
                if (oldcomm.ExecuteScalar() != null)
                {
    
    
                    MessageBox.Show("你输入的班级编号已经存在,请重新输入!", "错误提示");

                }
                else
                {
    
                    
                sql = "insert into ClassInfo  values ('" + textBox1.Text.Trim() + "','" + textBox2.Text.Trim() + "','" + textBox3.Text.Trim() + "','" + textBox4.Text.Trim() + "','" + textBox5.Text.Trim() + "','" + textBox6.Text.Trim() +"' )";
                oldcomm.CommandText = sql;
                oldcomm.ExecuteNonQuery();
                MessageBox.Show("添加成功!", "提示");
                DB.cn.Close();

        }
        oleDbConnection1.Close();

      }

   }

        private void AddClass_Load(object sender, EventArgs e)
        {
    
           

            DataSet ds = new DataSet();
            OleDbDataAdapter adp = new OleDbDataAdapter("", DB.cn);
            adp.SelectCommand.CommandText = "select ClassName from ClassInfo";
            adp.Fill(ds);
            textBox1.Focus();
        }      

       
    }
}

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 学生学籍管理系统
{
    
    
    public partial class CourseView : Form
    {
    
    
        public CourseView()
        {
    
    
            InitializeComponent();
        }

        private void courseInfoBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
    
    
            this.Validate();
            this.courseInfoBindingSource.EndEdit();
            this.courseInfoTableAdapter.Update(this.xsxjdbDataSet.courseInfo);

        }
        
        private void CourseView_Load(object sender, EventArgs e)
        {
    
    
            // TODO: 这行代码将数据加载到表“xsxjdbDataSet.courseInfo”中。您可以根据需要移动或移除它。
            //this.courseInfoTableAdapter.Fill(this.xsxjdbDataSet.courseInfo);
            // TODO: 这行代码将数据加载到表“xsxjdbDataSet.courseInfo”中。您可以根据需要移动或移除它。
            this.courseInfoTableAdapter.Fill(this.xsxjdbDataSet.courseInfo);

        }

        private void button1_Click(object sender, EventArgs e)
        {
    
                                                                                                                                         
            DataView dv = xsxjdbDataSet.courseInfo.DefaultView;
            dv.Sort = "SpeNum";
            dv.RowFilter = "SpeNum='" + textBox1.Text.Trim() + "'";
            dv.RowStateFilter = DataViewRowState.CurrentRows;
            courseInfoDataGridView.DataSource = dv;
        }

        private void button2_Click(object sender, EventArgs e)
        {
    
    
            DataView dv = xsxjdbDataSet.courseInfo.DefaultView;
            dv.Sort = "";
            dv.RowFilter = "";
            dv.RowStateFilter = DataViewRowState.CurrentRows;
            courseInfoDataGridView.DataSource = dv;
        }

        private void courseInfoBindingNavigatorSaveItem_Click_1(object sender, EventArgs e)
        {
    
    
            this.Validate();
            this.courseInfoBindingSource.EndEdit();
            this.courseInfoTableAdapter.Update(this.xsxjdbDataSet.courseInfo);

        }
    }
}

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 学生学籍管理系统
{
    
    
    public partial class Deletecourse : Form
    {
    
    
        public Deletecourse()
        {
    
    
            InitializeComponent();
        }

        private void courseInfoBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
    
    
            this.Validate();
            this.courseInfoBindingSource.EndEdit();
            this.courseInfoTableAdapter.Update(this.xsxjdbDataSet.courseInfo);

        }

        private void Dealcourse_Load(object sender, EventArgs e)
        {
    
    
            // TODO: 这行代码将数据加载到表“xsxjdbDataSet.courseInfo”中。您可以根据需要移动或移除它。
            this.courseInfoTableAdapter.Fill(this.xsxjdbDataSet.courseInfo);

        }

        private void button2_Click(object sender, EventArgs e)
        {
    
    
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
    
    
            DB.cn.Close();
            DB.cn.Open();
            string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xsxjdb.mdb";
            OleDbConnection  sqlconn = new OleDbConnection (connstring);
            sqlconn.Open();
            string sqlstring = "delete from CourseInfo where CourseName='" + textBox1.Text.Trim() + "'";
            
            OleDbCommand  sqlcomm = new OleDbCommand (sqlstring, sqlconn);
            sqlcomm.ExecuteNonQuery();
            MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            textBox1.Clear();
            textBox1.Focus();
        }

        private void button3_Click(object sender, EventArgs e)
        {
    
    
            if (xsxjdbDataSet.HasChanges())
            {
    
    
                DialogResult r = MessageBox.Show("真的修改吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (r == DialogResult.Yes)
                {
    
    

                    this.Validate();
                    this.courseInfoBindingSource.EndEdit();
                    this.courseInfoTableAdapter.Update(this.xsxjdbDataSet.courseInfo);
                
                }
            
            }
        }

       
    }
}

在这里插入图片描述

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data .OleDb ;

namespace 学生学籍管理系统
{
    
    
    public partial class Scoreupdate : Form
    {
    
    
        CurrencyManager cm;
        public Scoreupdate()
        {
    
    
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
    
    

            
            }

        

        private void button2_Click(object sender, EventArgs e)
        {
    
    

        }

        private void Scoreupdate_Load(object sender, EventArgs e)
        {
    
    
            string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xsxjdb.mdb";
            string sqlstring = "select * from ScoreInfo ";
            OleDbConnection oldconn = new OleDbConnection(connstring);
            oldconn.Open();

            OleDbDataAdapter oda = new OleDbDataAdapter(sqlstring, connstring);
            DataSet ds = new DataSet();
            oda.Fill(ds, "ScoreInfo");
            
            textBox1.DataBindings.Add("Text", ds, "ScoreInfo.Num");
            textBox2.DataBindings.Add("Text", ds, "ScoreInfo.Name");
            textBox3.DataBindings.Add("Text", ds, "ScoreInfo.ClassName");
            textBox4.DataBindings.Add("Text", ds, "ScoreInfo.CourseName");
            textBox5.DataBindings.Add("Text", ds, "ScoreInfo.Fraction");
            textBox6.DataBindings.Add("Text", ds, "ScoreInfo.CourseType");
            textBox7.DataBindings.Add("Text", ds, "ScoreInfo.State");

            cm = (CurrencyManager)this.BindingContext[ds, "ScoreInfo"];
            cm.Position = 0;
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
    
    
            cm.Position = 0;
        }

        private void button5_Click(object sender, EventArgs e)
        {
    
    
            this.cm.Position = this.cm.Count - 1; 

        }

        private void button3_Click(object sender, EventArgs e)
        {
    
    
            
            if (this.cm.Position == 0)
            {
    
    
                MessageBox.Show("已经是第一条记录", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
                this.cm.Position--;
		
        }

        private void button4_Click(object sender, EventArgs e)
        {
    
    
            
            if (this.cm.Position == this.cm.Count - 1)
            {
    
    
                MessageBox.Show("已经是末一条记录", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
                this.cm.Position++;
        }

        private void button6_Click(object sender, EventArgs e)
        {
    
    
            string upstring = "update ScoreInfo set Name='"+ textBox2.Text.Trim() + "',ClassName='" + textBox3.Text.Trim() + "',CourseName='"+ textBox4.Text.Trim() + "',Fraction='"+ textBox5.Text.Trim() + "',CourseType='"+ textBox7.Text.Trim() + "',State='"+ textBox6.Text.Trim() + "' where Num='" + textBox1.Text.Trim() + "'";
           
            string connstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=xsxjdb.mdb";
            OleDbConnection oldconn = new OleDbConnection(connstring);
            oldconn.Open();
            OleDbCommand oldcomm = new OleDbCommand(upstring, oldconn);
            oldcomm.ExecuteNonQuery();
            MessageBox.Show("更新成功!");

        }

        private void button7_Click(object sender, EventArgs e)
        {
    
    
            this.Close();
        } 


    }
}        

在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 学生学籍管理系统
{
    
    
    public partial class ScoreView : Form
    {
    
    
        public ScoreView()
        {
    
    
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
    
    
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
    
    
            string connstring = "provider=microsoft.Jet.OLEDB.4.0;DaTa Source=xsxjdb.mdb";
            string Sqlstring = "select Num as 学号,Name as 姓名,ClassName as 教师名,CourseName as 课程名,Fraction as 分数,CourseType as 课程类型,State as 状态 from ScoreInfo where Fraction>=400";
            OleDbConnection oldconn = new OleDbConnection(connstring);
            oldconn.Open();
            OleDbDataAdapter oda = new OleDbDataAdapter(Sqlstring, connstring);
            DataSet ds = new DataSet();
            oda.Fill(ds, "ScoreInfo");
            dataGridView1.DataSource = ds.Tables["ScoreInfo"];
           string  sql = "select count(*) from ScoreInfo where Fraction>=400";
            label1.Text ="及格人数有"+sql.ToString () ;
        }

        private void button3_Click(object sender, EventArgs e)
        {
    
    
            string connstring = "provider=microsoft.Jet.OLEDB.4.0;DaTa Source=xsxjdb.mdb";
            string Sqlstring = "select Num as 学号,Name as 姓名,ClassName as 教师名,CourseName as 课程名,Fraction as 分数,CourseType as 课程类型,State as 状态 from ScoreInfo where Fraction<=400";
            OleDbConnection oldconn = new OleDbConnection(connstring);
            oldconn.Open();
            OleDbDataAdapter oda = new OleDbDataAdapter(Sqlstring, connstring);
            DataSet ds = new DataSet();
            oda.Fill(ds, "ScoreInfo");
            dataGridView1.DataSource = ds.Tables["ScoreInfo"];
            string sql = "select count(*) from ScoreInfo where Fraction<=400";
            label1.Text = "不及格人数有" + sql.ToString();
        }

        private void ScoreView_Load(object sender, EventArgs e)
        {
    
    
            string connstring = "provider=microsoft.Jet.OLEDB.4.0;DaTa Source=xsxjdb.mdb";
            string Sqlstring = "select Num as 学号,Name as 姓名,ClassName as 教师名,CourseName as 课程名,Fraction as 分数,CourseType as 课程类型,State as 状态 from ScoreInfo";
            OleDbConnection oldconn = new OleDbConnection(connstring);
            oldconn.Open();
            OleDbDataAdapter oda = new OleDbDataAdapter(Sqlstring, connstring);
            DataSet ds = new DataSet();
            oda.Fill(ds, "ScoreInfo");
            dataGridView1.DataSource = ds.Tables["ScoreInfo"];
            label1.Text = "";
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
    
    
            string connstring = "provider=microsoft.Jet.OLEDB.4.0;DaTa Source=xsxjdb.mdb";
            string Sqlstring = "select Num as 学号,Name as 姓名,ClassName as 教师名,CourseName as 课程名,Fraction as 分数,CourseType as 课程类型,State as 状态 from ScoreInfo where CourseType='" + comboBox1.Text.Trim() + "'";
            OleDbConnection oldconn = new OleDbConnection(connstring);
            oldconn.Open();
            OleDbDataAdapter oda = new OleDbDataAdapter(Sqlstring, connstring);
            DataSet ds = new DataSet();
            oda.Fill(ds, "ScoreInfo");
            dataGridView1.DataSource = ds.Tables["ScoreInfo"];
           
        }

        private void button4_Click(object sender, EventArgs e)
        {
    
    
            this.Close();
        }
    }
}

四、总结
博主在设计这个学生学籍管理系统的时候也遇到了一些困难,不过都慢慢解决了,至于体会与心得,由于时间仓促,这里就不一一和大家分享了。大家可以关注我的博客,我会不定期的给大家分享文章,欢迎大家关注和留言哦!

猜你喜欢

转载自blog.csdn.net/m0_46374969/article/details/111873157