现需要完成一个简单学生信息管理系统的设计。具体要求如下。
(1)界面要求
运行程序的主界面如下所示:
录入信息后的效果:
各种系统提示:
(2)实现步骤
创建Mysql数据库StudentDB(SQL源码见后文,数据均为虚构)
设计窗体功能:
· 主界面按效果图添加相应控件与布局
· 默认打开窗体后查询所有学生信息
· 点击增加按钮能正确的查询插入一条学生信息并弹出对应提示信息,同时刷新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', '山西榆次');
/*
* 作者: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());
}
}
}
【测试结果】
运行程序,显示如下界面:
测试添加数据,输入需要插入的学生信息,并点击“添加”按钮,系统弹出添加成功提示窗口,主界面下方列表视图完成同步更新。
测试删除数据,使用鼠标在下方列表视图中选中需要除去的学生对应行,点击“删除”按钮,系统弹出删除成功提示窗口,主界面下方列表视图完成同步更新。