目录
1.2在Vsiual studio 2017初始界面点击文件->新建->项目,找到Windows窗体应用,修改项目名为xscj,点击保存
https://pan.baidu.com/s/104uMaDW65VMTitiGYKMliw
前言
本实验使用Windows窗体应用程序,基于.NET4.6.1。采用Visual C#编程语言来编写并实现“学生信息管理系统”,开发工具用到Visual Studio 2017,以MySQL8.0.26作为后台数据库。
一、前期准备
1.创建新项目并连接数据库
1.1下载MySQL
官方网站:https://www.mysql.com/cn/
1.2在Vsiual studio 2017初始界面点击文件->新建->项目,找到Windows窗体应用,修改项目名为xscj,点击保存
建好以后在点击新项目,在界面上方找到并点击项目->添加引用->扩展,在扩展里找到Mysql.Data并选勾选即可。
1.3 连接数据库的4个主要对象
对象名 | 作用 |
---|---|
Connection | 用于建立与特定数据库的连接 |
Commdand | 对数据源操作命令的封装 |
DataReader | 对特定的数据源的数据进行只读的数据访问 |
DataAdapter | 数据适配器,用于连接对象(Connection)连接数据源 |
2.MySQL表格创建
2.1 主要表格
学生表,成绩表,课程表,管理员表,学生登录表、
建表可以在phpMyadmin可视化操作,也可以在终端进行(也可以使用mysql可视化工具),可参考本文中的数据类型。
2.2 学生表
功能:记录学生的学号,姓名,性别,出生时间,已修课程数,照片。
2.3成绩表
功能:记录学生的各课程成绩,并通过学号查询。
2.4课程表
功能:记录已开课程,方便记录与修改课程。
2.5教师表
功能:记录老师的账号与密码以及姓名。
2.6学生登入表
功能:记录每个学生的姓名与密码。每位学生的密码默认为:1008611
2. 7建立触发器
2.7.1 CJ表与XS表
功能:在成绩表中添加(删除)成绩时,修改学生表中对应课程数的值。
2.7.2 XS表与USER_STU表
功能:在老师添加(删除)学生时,对应的在学生登入表中添加(删除)该学生账号。
二、窗体设置
通过添加控件并布局可实现窗体界面,读者可按需求自行设置窗体。
如下图
本实验中需要对DataGridView控件中多个属性进行修改
为了使学生照片能在PictureBox控件中完整显示:可在PictureBox控件中将其属性名SizeMode的属性值修改成:Stretchimage.
注意:除了Boutton控件外,其他控件千万千万双击控件!!!!!!!!!!!!!
为了清楚分辨各控件可以修改其属性(NAME)的值。
三、部分功能的实现(附源码)
1 登入界面
1.1 连接MySql数据库
static string config = "database=pxscj;" +
"datasource = 127.0.0.1;" +
"userid = root;" +
"port=3306;" +
"password=*******"; //数据库连接字段
MySqlConnection conn = new MySqlConnection(config); //连接数据库
1.2登入功能实现
代码实现
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 MySql.Data.MySqlClient;
.......部分代码.......
// 登入按钮事件响应
private void Enter_button_Click(object sender, EventArgs e)
{
try
{ conn.Open(); }
catch
{
MessageBox.Show("数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
if (name_textBox.Text== "")
{
MessageBox.Show("用户名不能为空", "用户登入", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else if(paw_textBox.Text == "")
{
MessageBox.Show("密码不能为空", "用户登入", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
string user_Id = name_textBox.Text;
string password = paw_textBox.Text;
string sql = "SELECT COUNT(*) FROM USER_STU WHERE ID='" + user_Id + "'AND PASSWORD='" + password + "';";
string sql1 = "SELECT COUNT(*) FROM USER_ADM WHERE ID='" + user_Id + "'AND PASSWORD='" + password + "';";
MySqlCommand cmd = new MySqlCommand(sql, conn);//查询
MySqlCommand cmd1 = new MySqlCommand(sql1, conn);//查询
if (Convert.ToInt32(cmd.ExecuteScalar()) == 1 ) // 转换 查找 找到为1
{
//进入学生界面
MessageBox.Show("登入成功", "欢迎使用", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
Stu stu = new Stu(user_Id);
stu.Show();
this.Hide();
}
else if(Convert.ToInt32(cmd1.ExecuteScalar()) == 1)
{
// 进入管理员界面
MessageBox.Show("登入成功", "欢迎使用", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
Adm adm = new Adm();
adm.Show();
this.Hide();
}
else //登入成功则直接进入信息界面
{
MessageBox.Show("账号或者密码错误,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
conn.Close();
}
return;
1.3 学生信息显示
代码实现
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 MySql.Data.MySqlClient;
using System.IO;
/*部分代码*/
namespace pxscj
{
public partial class Stu : Form
{
static string config = "database=pxscj;" +
"datasource = 127.0.0.1;" +
"userid = root;" +
"port=3306;" +
"password=******"; //数据库连接字段
private static string path = ""; //照片文件的路径
MySqlConnection conn = new MySqlConnection(config);
public Stu(string id)
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
label1.Text = id;
try
{ conn.Open(); }
catch
{
MessageBox.Show("数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.Hide();
return;
}
}
//显示所有
private void Stu_Load(object sender, EventArgs e)
{
//显示个人信息
DataSet myDs = new DataSet();
string mySqlStr = "select * from XS where ID=" + label1.Text.ToString() + ";";
MySqlCommand cmd = new MySqlCommand(mySqlStr, conn); //语句
MySqlDataReader dr = cmd.ExecuteReader(); //用DataReader 读数据
if (dr.Read())
{
label2.Text = dr.GetString(1).ToString();//将数据传入各个框中
if (dr.GetString(2).ToString() == "1")// 判断性别
label3.Text = "男";
else
label3.Text = "女";
label4.Text = dr.GetString(3).ToString();
label5.Text = dr.GetString(4).ToString();
}
dr.Close();
// 显示所学课程名
}
}
2 学生管理主界面
代码实现
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 MySql.Data.MySqlClient;
namespace pxscj
{
public partial class Choose : Form
{
static string conflg = "database=pxscj;" +
"datasource = 127.0.0.1;" +
"userid = root;" +
"port=3306;" +
"password=*******"; //数据库连接字段
MySqlConnection conn = new MySqlConnection(conflg);
static string sno = "";//接收学号文本框
public Choose()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
try
{ conn.Open(); }
catch
{
MessageBox.Show("数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
}
//查看学生
private void button1_Click(object sender, EventArgs e)
{
if (textBox_sno.Text != "")
{
string str = "SELECT * FROM XS WHERE ID ='" + textBox_sno.Text.ToString() + "';";
MySqlCommand pcmd = new MySqlCommand(str, conn);
MySqlDataReader dr = pcmd.ExecuteReader(); //用DataReader 读数据
if (dr.Read()) //判断是否有该学生
{
sno = textBox_sno.Text.ToString();
Stu stu = new Stu(sno);
stu.Show();
}
else
{
MessageBox.Show("无此学生!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
dr.Close();
}
else
{
MessageBox.Show("请输入需要查看的学生学号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
// 添加学生
private void button2_Click(object sender, EventArgs e)
{
Addstu add = new Addstu();
add.Show();
NEW_LOAD(null, null);
}
//修改学生
private void button3_Click(object sender, EventArgs e)
{
}
//删除该学生
private void button4_Click(object sender, EventArgs e)
{
}
//显示所有学生
private void Choose_Load(object sender, EventArgs e)
{
NEW_LOAD(null, null);
}
public void NEW_LOAD(object sender,EventArgs e)
{
string kcm = "SELECT ID AS 学号, XM AS 姓名, CSSJ AS 出生时间, KCS AS 课程数 FROM XS";
MySqlDataAdapter da = new MySqlDataAdapter(kcm, conn);// 打开数据适配器(用于读取数据)
DataSet ds = new DataSet();//创建数据表
da.Fill(ds);
dataGridView_stu.DataSource = ds.Tables[0];// 把dataset 与dataGridView_xmcj数据绑定一起
textBox_sno.Text = "";
}
private void button5_Click_1(object sender, EventArgs e)
{
conn.Close();
this.Hide();
return;
}
}
}
2.1修改学生功能实现
代码实现
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.IO;
using MySql.Data.MySqlClient;
namespace pxscj
{
public partial class Upstu : Form
{
static string config = "database=pxscj;" +
"datasource = 127.0.0.1;" +
"userid = root;" +
"port=3306;" +
"password=******"; //数据库连接字段
private static string path = ""; //照片文件的路径
MySqlConnection conn = new MySqlConnection(config); //连接数据库
public Upstu(string id)
{
InitializeComponent();
label2.Text = id;
this.StartPosition = FormStartPosition.CenterScreen;
try
{ conn.Open(); }
catch
{
MessageBox.Show("数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
}
private void Upstu_Load(object sender, EventArgs e)
{
}
// 修改信息
private void button_yes_Click(object sender, EventArgs e)
{
try
{
// 判断信息是否完整
if (textBox_name.Text == "")
{
MessageBox.Show("姓名不能为空", "用户更新", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else if (textBox_cssj.Text == "")
{
MessageBox.Show("出生时间不能为空", "用户更新", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else if (!radioButton_male.Checked && !radioButton_female.Checked)
{
MessageBox.Show("性别不能为空", "用户更新", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
// 保存字段
string name = textBox_name.Text.ToString();
string cssj = textBox_cssj.Text.ToString();
int xb = 0;//默认性别为女
if (radioButton_male.Checked) xb = 1;
// 判断有无照片插入(默认为无照片)
string str = "UPDATE XS SET XM='" + name + "',XB='" + xb + "',CSSJ='" + cssj + "' WHERE ID='" + label2.Text + "'";
if (path != "")
str = "UPDATE XS SET XM='" + name + "',XB='" + xb + "',CSSJ='" + cssj + "',ZP=@Photo WHERE ID='" + label2.Text + "'";
MySqlCommand cmd = new MySqlCommand(str, conn);
if (path != "")
{
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);//将图片以文件流的形式保存
byte[] fileBytes = new byte[fs.Length];//创建字节数组
BinaryReader br = new BinaryReader(fs);
fileBytes = br.ReadBytes(Convert.ToInt32(fs.Length));
MySqlParameter mpar = new MySqlParameter("@Photo", SqlDbType.Image);//为命令创建参数
mpar.MySqlDbType = MySqlDbType.VarBinary;
mpar.Value = fileBytes; //为参数赋值
cmd.Parameters.Add(mpar);// 添加参数
}
//在MYSQl中执行该语句
cmd.ExecuteNonQuery();
MessageBox.Show("修改成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
this.Hide();
}
}
catch
{
MessageBox.Show("修改失败,请检查信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
return;
}
//选择接受照片
private void button_selectphoto_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
//设置接受图片的格式
openDialog.Filter = "bmp 文件(*.bmp)|bmp|gif 文件(*gif)|*.gif|jpeg 文件(*jpg)|*.jpg";
openDialog.FilterIndex = 3;
openDialog.Title = "选择照片";//设置对话框的名称
if (openDialog.ShowDialog() == DialogResult.OK)
path = openDialog.FileName;//获取路径
pictureBox_stu.Image = Image.FromFile(path);
MessageBox.Show(path);//测试是否保存该路径
return;
}
private void button1_Click(object sender, EventArgs e)
{
conn.Close();
this.Hide();
return;
}
}
}
2.2添加学生功能实现
代码实现
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.IO;
using MySql.Data.MySqlClient;
namespace pxscj
{
public partial class Addstu : Form
{
static string config = "database=pxscj;" +
"datasource = 127.0.0.1;" +
"userid = root;" +
"port=3306;" +
"password=*******"; //数据库连接字段
private static string path = ""; //照片文件的路径
public Addstu()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
}
// 添加学生
private void button_yes_Click(object sender, EventArgs e)
{
try
{
MySqlConnection conn = new MySqlConnection(config); //连接数据库
conn.Open(); //打开数据库
// 判断信息是否完整
if (textBox_id.Text == "")
{
MessageBox.Show("学号不能为空", "用户登入", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else if (textBox_name.Text == "")
{
MessageBox.Show("姓名不能为空", "用户登入", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else if (textBox_cssj.Text == "")
{
MessageBox.Show("出生时间不能为空", "用户登入", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else if (!radioButton_male.Checked && !radioButton_female.Checked)
{
MessageBox.Show("性别不能为空", "用户登入", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
// 保存字段
string id = textBox_id.Text.ToString();
string name = textBox_name.Text.ToString();
string cssj = textBox_cssj.Text.ToString();
int xb = 0;//默认性别为女
if (radioButton_male.Checked) xb = 1;
// 判断有无照片插入(默认为有照片)
string str = "INSERT INTO XS VALUES('" + id + "','" + name + "','" + xb + "','" + cssj + "',0,NULL)";
if (path != "")
str = "INSERT INTO XS VALUES('" + id + "','" + name + "','" + xb + "','" + cssj + "',0,@Photo)";
MySqlCommand cmd = new MySqlCommand(str, conn);
if (path!="")
{
FileStream fs = new FileStream(path, FileMode.Open,FileAccess.Read);//将图片以文件流的形式保存
byte[] fileBytes = new byte[fs.Length];//创建字节数组
BinaryReader br = new BinaryReader(fs);
fileBytes = br.ReadBytes(Convert.ToInt32(fs.Length));
MySqlParameter mpar = new MySqlParameter("@Photo", SqlDbType.Image);//为命令创建参数
mpar.MySqlDbType = MySqlDbType.VarBinary;
mpar.Value = fileBytes; //为参数赋值
cmd.Parameters.Add(mpar);// 添加参数
}
//在MYSQl中执行该语句
cmd.ExecuteNonQuery();
MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
conn.Close();
this.Hide();
}
}
catch
{
MessageBox.Show("添加失败,请检查信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
return;
}
// 选择照片按钮事件
private void button_selectphoto_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
//设置接受图片的格式
openDialog.Filter = "bmp 文件(*.bmp)|bmp|gif 文件(*gif)|*.gif|jpeg 文件(*jpg)|*.jpg";
openDialog.FilterIndex = 3;
openDialog.Title = "选择照片";//设置对话框的名称
if (openDialog.ShowDialog() == DialogResult.OK)
path = openDialog.FileName;//获取路径
pictureBox_stu.Image = Image.FromFile(path);
//MessageBox.Show(path);//测试是否保存该路径
}
}
}
3.成绩管理主界面功能实现
代码实现
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 MySql.Data.MySqlClient;
namespace pxscj
{
public partial class CJstu : Form
{
static string conflg = "database=pxscj;" +
"datasource = 127.0.0.1;" +
"userid = root;" +
"port=3306;" +
"password=*********"; //数据库连接字段
MySqlConnection conn = new MySqlConnection(conflg);
public CJstu()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
//数据库连接
try
{ conn.Open(); }
catch
{
MessageBox.Show("数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
}
// 删除成绩
private void button4_Click(object sender, EventArgs e)
{
if (textBox_Sno.Text != "")
{
string str = "SELECT * FROM XS WHERE ID ='" + textBox_Sno.Text.ToString() + "';";
MySqlCommand pcmd = new MySqlCommand(str, conn);
MySqlDataReader dr = pcmd.ExecuteReader(); //用DataReader 读数据
if (dr.Read()) //判断是否有该学生
{
DelCJ dj = new DelCJ(textBox_Sno.Text.ToString());
dj.Show();
}
else
{
MessageBox.Show("无此学生!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
dr.Close();
}
else
{
MessageBox.Show("请输入需要查看的学生学号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
NEW_LOAD(null, null);
}
// 添加成绩
private void button2_Click(object sender, EventArgs e)
{
if (textBox_Sno.Text != "")
{
string str = "SELECT * FROM XS WHERE ID ='" + textBox_Sno.Text.ToString() + "';";
MySqlCommand pcmd = new MySqlCommand(str, conn);
MySqlDataReader dr = pcmd.ExecuteReader(); //用DataReader 读数据
if (dr.Read()) //判断是否有该学生
{
AddCJ ac = new AddCJ(textBox_Sno.Text.ToString());
ac.Show();
}
else
{
MessageBox.Show("无此学生!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
dr.Close();
}
else
{
MessageBox.Show("请输入需要查看的学生学号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
NEW_LOAD(null, null);
}
// 课程管理
private void button3_Click(object sender, EventArgs e)
{
UpKC kc = new UpKC();
kc.Show();
NEW_LOAD(null, null);
return;
}
//页面加载
private void CJstu_Load(object sender, EventArgs e)
{
NEW_LOAD(null, null);
}
// 显示所有学生的已学的课程
public void NEW_LOAD(object sender, EventArgs e)
{
string kcm = "SELECT XS.ID AS 学号, XS.XM AS 姓名,KC.KCM AS 已修课程,CJ.CJ AS 成绩 FROM XS,CJ,KC WHERE XS.ID=CJ.ID AND CJ.KCH=KC.KCH";
MySqlDataAdapter da = new MySqlDataAdapter(kcm, conn);// 打开数据适配器(用于读取数据)
DataSet ds = new DataSet();//创建数据表
da.Fill(ds);
dataGridView_stu.DataSource = ds.Tables[0];// 把dataset 与dataGridView_xmcj数据绑定一起
textBox_Sno.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
conn.Close();
this.Hide();
return;
}
}
}
3.1添加成绩
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 MySql.Data.MySqlClient;
namespace pxscj
{
public partial class AddCJ : Form
{
static string conflg = "database=pxscj;" +
"datasource = 127.0.0.1;" +
"userid = root;" +
"port=3306;" +
"password=******"; //数据库连接字段
MySqlConnection conn = new MySqlConnection(conflg);// 数据库连接
public AddCJ(string id)
{
InitializeComponent();
label2.Text = id;
this.StartPosition = FormStartPosition.CenterScreen;
try
{ conn.Open(); }
catch
{
MessageBox.Show("数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
}
//初始化加载课程名
private void AddCJ_Load(object sender, EventArgs e)
{
string str = "SELECT KCM FROM KC";
MySqlDataAdapter pcmd = new MySqlDataAdapter(str, conn);
DataSet da = new DataSet();
comboBox_KC.Items.Add("请选择");
pcmd.Fill(da, "KCM");
for(int i = 0; i<da.Tables["KCM"].Rows.Count;i++)
{
comboBox_KC.Items.Add(da.Tables["KCM"].Rows[i][0].ToString());
}
comboBox_KC.SelectedIndex = 0;//默认初始显示
textBox_cj.Text = "";
return;
}
private void comboBox_KC_SelectedIndexChanged(object sender, EventArgs e)
{
}
// 确定按钮事件
private void button_yes_Click(object sender, EventArgs e)
{
try
{
string cj = textBox_cj.Text.ToString();
//接收combox的选择结果
int kc = comboBox_KC.SelectedIndex;
string str = "INSERT INTO CJ VALUES('" + label2.Text + "','" + kc + "','" + cj + "')";
MySqlCommand cmd = new MySqlCommand(str, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
catch
{
MessageBox.Show("添加失败!请检查信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
}
private void button1_Click(object sender, EventArgs e)
{
conn.Close();
this.Hide();
}
}
}
3.2添加课程
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 MySql.Data.MySqlClient;
namespace pxscj
{
public partial class UpKC : Form
{
static string conflg = "database=pxscj;" +
"datasource = 127.0.0.1;" +
"userid = root;" +
"port=3306;" +
"password=55555"; //数据库连接字段
MySqlConnection conn = new MySqlConnection(conflg);
static string id = ""; //保存课程数
public UpKC()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
try
{ conn.Open(); }
catch
{
MessageBox.Show("数据库连接失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
}
// 添加课程
private void button_yes_Click(object sender, EventArgs e)
{
if (textBox_kc.Text == "")
{
MessageBox.Show("请输入课程名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
try
{
string kc_name = textBox_kc.Text.ToString();
string str = "INSERT INTO KC VALUES('" + id + "', '" + kc_name + "',NULL);";// 插入语句
MySqlCommand kcmd = new MySqlCommand(str, conn);
kcmd.ExecuteNonQuery();
MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
NEW(null, null);
}
catch
{
MessageBox.Show("添加失败!已有该课程", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
return;
}
//删除课程
private void button2_Click(object sender, EventArgs e)
{
if (textBox_kc.Text == "")
{
MessageBox.Show("请输入课程名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
try
{
string kc_name = textBox_kc.Text;
string str = "DELETE FROM KC WHERE KCM='" + kc_name + "';";
MySqlCommand cmd = new MySqlCommand(str, conn);
MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
cmd.ExecuteNonQuery();
NEW(null, null);
}
catch
{
MessageBox.Show("删除失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}
return;
}
//加载所有课程
private void UpKC_Load(object sender, EventArgs e)
{
NEW(null, null);
return;
}
private void NEW(object sender,EventArgs e)
{
string kcm = "SELECT KCM AS 课程名 FROM KC ";
MySqlDataAdapter da = new MySqlDataAdapter(kcm, conn);// 打开数据适配器(用于读取数据)
DataSet ds = new DataSet();//创建数据表
da.Fill(ds);
dataGridView_kc.DataSource = ds.Tables[0];// 把dataset 与dataGridView_xmcj数据绑定一起
id= dataGridView_kc.Rows.Count.ToString();//获取总课程数
textBox_kc.Text = "";
return;
}
private void button1_Click(object sender, EventArgs e)
{
conn.Close();
this.Hide();
return;
}
}
}
完整项目源码链接
项目分享
提取码:YY55
总结
新人第一次写文章,可能文笔不好,大佬有什么建议可以在评论区留下您宝贵的建议,这对我真的很重要!!!!!!!!!!!!