【C#】基于C#和MySQL的简单学生信息管理系统设计

现需要完成一个简单学生信息管理系统的设计。具体要求如下。

(1)界面要求

运行程序的主界面如下所示:
在这里插入图片描述
录入信息后的效果:
在这里插入图片描述
各种系统提示:
在这里插入图片描述
在这里插入图片描述
(2)实现步骤

  1. 创建Mysql数据库StudentDB(SQL源码见后文,数据均为虚构)

  2. 添加学生信息表Stud,各字段要求如下。
    在这里插入图片描述

  3. 设计窗体功能:
    · 主界面按效果图添加相应控件与布局
    · 默认打开窗体后查询所有学生信息
    · 点击增加按钮能正确的查询插入一条学生信息并弹出对应提示信息,同时刷新DataGridView数据控件。
    · DataGridView控件选中一行后点击删除按钮可以正确删除该条学生信息并弹出对应提示信息,同时刷新DataGridView数据控件。

【SQL源码】

CREATE DATABASE IF NOT EXISTS `studentdb`;
USE `studentdb`;

CREATE TABLE IF NOT EXISTS `stud` (
  `sno` char(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL,
  `className` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  `sname` varchar(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  `sex` char(1) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  `birth` date DEFAULT NULL,
  `tel` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  `address` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL,
  PRIMARY KEY (`sno`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

INSERT INTO `stud` (`sno`, `className`, `sname`, `sex`, `birth`, `tel`, `address`) VALUES
	('20210001', '通讯211', '韩光', '男', '2001-05-29', '15311431235', '湖北荆州'),
	('20210002', '电子212', '李响', '女', '2002-10-08', '13917892341', '河北邢台'),
	('20210003', '通讯211', '刘晓丽', '女', '2001-10-21', '17318792113', '山西榆次');

【窗体设计】
在这里插入图片描述
【MainForm.cs】

/*
 * 作者:JeronZhou
 * 时间:2021-11-01
 * 功能:简单学生信息管理系统
 */

using System;
using System.Data;
using MySql.Data.MySqlClient;
using System.Windows.Forms;

namespace Teat2_3
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}
		void MainFormLoad(object sender, EventArgs e)
		{
			string Connection = "data source=localhost;database=studentdb;user id=用户名;password=密码;pooling=true;charset=utf8;";
        	using(MySqlConnection msc = new MySqlConnection(Connection))
        	{
        		msc.Open();
            	string sql = "select * from stud;";
                MySqlCommand cmd = new MySqlCommand(sql, msc);
                MySqlDataAdapter adapt = new MySqlDataAdapter();
                adapt.SelectCommand = cmd;
                DataSet dataset = new DataSet();
                adapt.Fill(dataset, "table");
                dataGridView1.DataSource = dataset.Tables["table"];
                cmd.ExecuteNonQuery();
                msc.Close();       	
        	}
		}
		void Button1Click(object sender, EventArgs e)
		{
            string Sno, ClassName, Sname, Sex, Birth, Tel, Address;
            string Connection = "data source=localhost;database=studentdb;user id=用户名;password=密码;pooling=true;charset=utf8;";
            using(MySqlConnection msc = new MySqlConnection(Connection))
            {
            	Sno = textBox1.Text;
            	ClassName = comboBox1.Text;
            	Sname = textBox2.Text;
            	Sex = "";
            	if(radioButton1.Checked)
            	{
            		Sex = radioButton1.Text;
            	}
            	else if(radioButton2.Checked)
            	{
            		Sex = radioButton2.Text;
            	}
            	Birth = dateTimePicker1.Text;
            	Tel = textBox3.Text;
            	Address = textBox4.Text;
            	
            	msc.Open();
            	string sql1 = "INSERT INTO stud values('" + Sno + "','" + ClassName + "','" + Sname + "','" + Sex + "','" + Birth + "','" + Tel + "','" + Address + "')";
                MySqlCommand cmd1 = new MySqlCommand(sql1, msc);
                cmd1.ExecuteNonQuery();
                msc.Close();
                
                MessageBox.Show("增加学生信息成功!");
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                comboBox1.Text = "";
                dateTimePicker1.Text = "";
                
                msc.Open();
            	string sql2 = "select * from stud;";
                MySqlCommand cmd2 = new MySqlCommand(sql2, msc);
                MySqlDataAdapter adapt = new MySqlDataAdapter();
                adapt.SelectCommand = cmd2;
                DataSet dataset = new DataSet();
                adapt.Fill(dataset, "table");
                dataGridView1.DataSource = dataset.Tables["table"];
                cmd2.ExecuteNonQuery();
                msc.Close(); 
			}
		}
		void Button2Click(object sender, EventArgs e)
		{
	        string Sno;
            string Connection = "data source=localhost;database=studentdb;user id=用户名;password=密码;pooling=true;charset=utf8;";
            using(MySqlConnection msc = new MySqlConnection(Connection))
            {
            	Sno = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            	
            	msc.Open();
            	string sql1 = "DELETE FROM stud WHERE stud.sno='" + Sno + "';";
                MySqlCommand cmd1 = new MySqlCommand(sql1, msc);
                cmd1.ExecuteNonQuery();
                msc.Close();
                
                MessageBox.Show("删除学生信息成功!");
                
                msc.Open();
            	string sql2 = "select * from stud;";
                MySqlCommand cmd2 = new MySqlCommand(sql2, msc);
                MySqlDataAdapter adapt = new MySqlDataAdapter();
                adapt.SelectCommand = cmd2;
                DataSet dataset = new DataSet();
                adapt.Fill(dataset, "table");
                dataGridView1.DataSource = dataset.Tables["table"];
                cmd2.ExecuteNonQuery();
                msc.Close(); 
			}
		}
	}
}

【Program.cs】

/*
 * 作者:JeronZhou
 * 时间:2021-11-01
 * 功能:简单学生信息管理系统
 */

using System;
using System.Windows.Forms;

namespace Teat2_3
{
	internal sealed class Program
	{
		[STAThread]
		private static void Main(string[] args)
		{
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			Application.Run(new MainForm());
		}
	}
}

【测试结果】

运行程序,显示如下界面:
在这里插入图片描述
测试添加数据,输入需要插入的学生信息,并点击“添加”按钮,系统弹出添加成功提示窗口,主界面下方列表视图完成同步更新。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
测试删除数据,使用鼠标在下方列表视图中选中需要除去的学生对应行,点击“删除”按钮,系统弹出删除成功提示窗口,主界面下方列表视图完成同步更新。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jeron Zhou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值