学生信息管理系统--数据库的使用开发Windows窗体C#大作业

学生信息管理系统
系统用户分三类:系统管理员、教师、学生,不同的用户有不同的使用权限。
系统管理员的使用权限包括:用户管理、学生信息管理、学生成绩查询,教师的使用权限包括学生信息查询、学生成绩管理、学生成绩查询,学生的使用权限仅有学生信息查询和学生成绩查询。
系统管理员帐号在创建用户表时建立,在应用程序中不能修改。教师的用户名和密码由系统管理员通过用户管理模块设定,学生信息由系统管理员通过学生信息管理模块来设定,学生成绩由教师通过学生成绩管理模块来设定。学生可通过系统登录模块用学号作为用户名进行注册,不存在的学号不能注册,注册时可设定用户密码,已注册的学生不能重新注册,也不能修改密码。学生登录系统后只可以查询本人的信息,但可查询所有学生的成绩。

1.这是大一接触学完的数据库和C#结合一起的操作系统,较为简单
2. 比较适合新手,过程就是不断的查询数据库和进行写入C#窗体
简单的思维导图

定义了六个表,一个最没有用的中间值表,其实严格是5个

mind是中间值表,StudentAd是管理员表,然后依次是成绩表,信息表,注册信息表,教师表

如果是我再次写,会写四个表,管理员表,成绩表,信息表,注册信息表

正式开始主界面分为在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using Conmon;

namespace Cshaper大作业学生信息管理系统
{
    public partial class Form1 : Form
    {
        public int str { get; private set; }

        public Form1()
        {
            InitializeComponent();
        }
        private void clera()
        {
            textBox1.Clear();
            textBox2.Clear();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
            string useName = textBox1.Text.Trim();
            string usepass = textBox2.Text.Trim();
            if (AdradioButton.Checked)
            {
                //管理员登录
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    string sql = string.Format("select * from StudentAd where Adname='{0}' and Adpass='{1}'", useName, usepass);
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        con.Open();
                       
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            //判断是否有返回值
                            if (reader.HasRows) 
                            {
                                MessageBox.Show("登录成功");
                                this.DialogResult = DialogResult.OK;
                                this.Close();

                            }
                            else
                            {
                                MessageBox.Show("登录失败");
                                clera();
                                textBox1.Focus();
                            }
                        }

                    }
                }
               
            }
            else if (TeradioButton2.Checked)
            {
                //Teacher登录
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    string sql = string.Format("select * from Teacher where UserID='{0}' and UserPassWord='{1}'", useName, usepass);
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        con.Open();
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            //判断是否有返回值
                            if (reader.HasRows)
                            {
                                MessageBox.Show("登录成功");
                                this.DialogResult = DialogResult.Yes;
                                this.Close();

                            }
                            else
                            {
                                MessageBox.Show("登录失败");
                                clera();
                                textBox1.Focus();
                            }
                        }

                    }
                }
                
            }
            else if (StradioButton3.Checked)
            {
                //学生登录
                using (SqlConnection con = new SqlConnection(conStr))
                {
                    string sql = string.Format("select * from studZcxx where student_id='{0}' and student_pss='{1}'", useName, usepass);
                    using (SqlCommand cmd = new SqlCommand(sql, con))
                    {
                        con.Open();
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            //判断是否有返回值
                            if (reader.HasRows)
                            {
                                MessageBox.Show("登录成功");

                                minds(useName);
                                this.DialogResult = DialogResult.No;
                                this.Close();

                            }
                            else
                            {
                                MessageBox.Show("登录失败");
                                clera();
                                textBox1.Focus();
                            }
                        }

                    }
                }
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Stzhuce stuXX = new Stzhuce();
            stuXX.ShowDialog();
        }


        //修改中间值  
        public int minds(string mins)
        {   
            int i = Convert.ToInt32(mins);
            string a1 = "update mind set mind='"+ i + "'";
            int n = SqlHelper.ExecuteNonquery(a1);

            return i;
        }
    }
}
!!这里的中间值是自己脑子发热,因为要通过学生登录了,进入学生登录页面,窗体传值又总是报错,于是直接通过数据库来写入一个中间数据表,来记录是谁进入了主页面,其实可以不用写的怎么麻烦,定义一个全局变量即可!!

在这里插入图片描述
管理员登录成功的界面

写完以后我进行修改,发现其实很多语句都可以进行封装
于是先定义了一个SqlHelper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace Conmon
{
    
    public class SqlHelper
    {//读取连接字符串
        private static string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        /// <summary>
        /// 返回数据集合
        /// </summary>
        /// <param name="sql">要执行查询的sql语句</param>
        /// <param name="param">需要的参数</param>
        /// <returns></returns>
        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] param)
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection(conStr))
            {
                SqlDataAdapter adapter = new SqlDataAdapter(sql, con);
                //添加参数
                adapter.SelectCommand.Parameters.AddRange(param);
                adapter.Fill(dt);
            }
            return dt;
        }
        /// <summary>
        /// 执行增删改
        /// </summary>
        /// <param name="sql">要执行的sql语句</param>
        /// <param name="param">sql语句需要的参数</param>
        /// <returns></returns>
        public static int ExecuteNonquery(string sql, params SqlParameter[] param)
        {
            int n = -1;
            using (SqlConnection con = new SqlConnection(conStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    con.Open();
                    cmd.Parameters.AddRange(param);
                    n = cmd.ExecuteNonQuery();
                }
            }
            return n;
        }
        /// <summary>
        /// 执行查询,返回首行首列
        /// </summary>
        /// <param name="sql">要查询的sql语句</param>
        /// <param name="param">sql语句需要的参数</param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql, params SqlParameter[] param)
        {
            object o = null;
            using (SqlConnection con = new SqlConnection(conStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    con.Open();
                    cmd.Parameters.AddRange(param);
                    o = cmd.ExecuteScalar();
                }
            }
            return o;
        }
        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] param)
        {
            SqlDataReader reader;
            SqlConnection con = new SqlConnection(conStr);
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                cmd.Parameters.AddRange(param);
                con.Open();
                //CommandBehavior.CloseConnection:如果关闭SqlDataReader,则Connection也随着关闭
                reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            }
            return reader;
        }
    }
}

再写管理员类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Conmon;

namespace Cshaper大作业学生信息管理系统
{
    public partial class Ad : Form
    {
        public Ad()
        {
            InitializeComponent();
        }


        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text=="" || textBox2.Text=="")
            {
                MessageBox.Show("用户名和密码不能为空");
                return;
            }
            string sql = "insert into Teacher(UserID,UserPassWord) values(@id,@word)";

            //Teacher

            SqlParameter[] p = new SqlParameter[]{
                        new SqlParameter("@id", Convert.ToString(textBox1.Text.Trim())),

                         new SqlParameter("@word", Convert.ToString(textBox2.Text)),
                        };
            int n = SqlHelper.ExecuteNonquery(sql, p);
            if (n > 0)
            {
                MessageBox.Show("添加成功!");
            }
            textBox1.Text = "";
            textBox2.Text = "";
            textBox1.Focus();

        }

      

        private void button3_Click(object sender, EventArgs e)
        {
            string drop1 = textBox3.Text.Trim();
            string sql = "DELETE FROM Teacher WHERE UserID = '"+ drop1 + "'";
            
            int n = SqlHelper.ExecuteNonquery(sql);
            if (n > 0)
            {
                MessageBox.Show("删除成功!");
            }
            else
            {
                MessageBox.Show("删除失败,请确定是否存在!!!");
            }
            textBox3.Text = "";
            textBox3.Focus();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string sql = "select * from Teacher";
            DataTable dt = SqlHelper.ExecuteDataTable(sql);
            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].HeaderCell.Value = "教师用户名";
            dataGridView1.Columns[1].HeaderCell.Value = "教师用户密码";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string ab1 = textBox4.Text.Trim();
            string ab2 = textBox5.Text.Trim();
            string sql = "update Teacher set UserID = '"+ab2+"' where UserID='"+ab1+"'";
            int n = SqlHelper.ExecuteNonquery(sql);
            if (n > 0)
            {
                MessageBox.Show("修改成功!");
            }
            else
            {
                MessageBox.Show("修改失败,请确定是否存在!!!");
            }
            //修改成功后可能要再进行修改密码,所有我们把要修改用户名的给清空
            textBox4.Text = ab2;
         
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string ab1 = textBox4.Text.Trim();
            string ab2 = textBox6.Text.Trim();
            string sql = "update Teacher set UserPassWord = '" + ab2 + "' where UserID='" + ab1 + "'";
            int n = SqlHelper.ExecuteNonquery(sql);
            if (n > 0)
            {
                MessageBox.Show("修改成功!");
            }
            else
            {
                MessageBox.Show("修改失败,请确定是否存在!!!");
            }
        }

        private void 学生信息查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StuXX stuXX = new StuXX();
            stuXX.ShowDialog();
        }

        private void 学生成绩查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            StuCJ stuXX = new StuCJ();
            stuXX.ShowDialog();
        }
    }
}




这里弹出的两个以对话框的形式弹出,附
StuCJ()

using Conmon;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Cshaper大作业学生信息管理系统
{
    public partial class StuCJ : Form
    {
        public StuCJ()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            string sql = "select * from studentCJ";
            DataTable dt = SqlHelper.ExecuteDataTable(sql);
            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].HeaderCell.Value = "学号";
            dataGridView1.Columns[1].HeaderCell.Value = "姓名";
            dataGridView1.Columns[2].HeaderCell.Value = "成绩";
        }

        private void StuCJ_Load(object sender, EventArgs e)
        {

        }
    }
}

StuXX类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Conmon;

namespace Cshaper大作业学生信息管理系统
{
    public partial class StuXX : Form
    {
        public StuXX()
        {
            InitializeComponent();
        }

        private void StuXX_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string sql = "select * from studentXX";
            DataTable dt = SqlHelper.ExecuteDataTable(sql);
            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].HeaderCell.Value = "学号";
            dataGridView1.Columns[1].HeaderCell.Value = "姓名";
            dataGridView1.Columns[2].HeaderCell.Value = "性别";
            dataGridView1.Columns[3].HeaderCell.Value = "年龄";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "")
            {
                MessageBox.Show("不能为空");
                return;
            }
            string a1 = textBox1.Text.Trim();
            string a2 = textBox2.Text.Trim();
            string a3 = textBox3.Text.Trim();
            string a4 = textBox4.Text.Trim();
            string sql = "insert into studentXX values('"+ a1 + "','" + a2 + "','" + a3 + "'," + a4 + ")";
            try
            {
                int n = SqlHelper.ExecuteNonquery(sql);
                if (n > 0)
                {
                    MessageBox.Show("添加成功!");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("请检查,学号唯一,姓名和学号不能为空,性别只能填男或女,年龄请认真填写");
            }
            textBox1.Text = "";
            textBox2.Text ="";
            textBox3.Text = "";
            textBox4.Text ="";
            textBox1.Focus();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string drop1 = textBox5.Text.Trim();
            string sql = "DELETE FROM studentXX WHERE student_id = '" + drop1 + "'";

            int n = SqlHelper.ExecuteNonquery(sql);
            if (n > 0)
            {
                MessageBox.Show("删除成功!");
                textBox5.Text = "";
                textBox5.Focus();
            }
            else
            {
                MessageBox.Show("删除失败,请确定是否存在!!!");
            }
        }
    }
}

这样我们的三分之一就完成了,后面的教师也是这样,总的来说是很简单的
就不占太多笔墨了
其中SQlHElpe类是需要学习的,学习完这个类其他大部分都能完成
数据库的使用的时候注意赋值的’ '这个是很重要的,在写代码的时候,建议先拿sql语句去运行看是否有返回结果和结果是否是自己想要的,在进行带入!
数据库里是可以运行触发器的,触发器是很重要的存在,我在写代码的时候想学生信息表和学生成绩表同步更新,最后自己使用了触发器可以解决!

归根本质还是对数据库的操作,然后通过下图去返回结果集

在这里插入图片描述

加油,华为!加油,中国!
努力学习,刻苦奋斗,做党的接班人!
为人民服务!!
努力写代码,刻苦去奋斗!未来是我们的

代码已经上传到GitHub,搜索对应标题学生信息管理系统,即可
不鼓励直接抄袭,鼓励刻苦学习

  • 10
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值