VS2019 C# MySQL 学生信息增删改查(一、增删) |
您所在的位置:网站首页 › 查学生成绩下载什么软件 › VS2019 C# MySQL 学生信息增删改查(一、增删) |
VS2019 C# MySQL 学生信息增删改查
由于刚刚学习VS2019,入手的第一个例子是编写一个简单的增删改查。这里,我以学生信息为例,进行详细的过程描述以及代码实现。
一、 新建一个Windows窗体应用程序 1、创建新项目界面,如下图所示。 1、将Form1属性中的Text的“Form1”改为“学生管理系统首页”,并从工具箱中拖入4个Button按钮放在设计界面上,如下图所示。分别将“button1”、“button2”、“button3”、“button4”属性中的(Name)改为“BtnZeng”、“BtnShan”、“BtnGai”、“BtnCha”,并分别将其属性中的Text改为“增加”、“删除”、“修改”、“查看”。 三、建立数据库 我用的是MySQL。 1、新建数据库,如下图所示。 1、在FormZengJia界面中拖入控件,如下图所示。
5、代码如下: private void BtnZeng_Click(object sender, EventArgs e) { String connectStr = "server=localhost;database=studentinfosystem;user=root;password=123456;port=3306;charset=utf8"; //server=127.0.0.1或localhost。database是我的数据库名,studentinfosystem是新建的数据库名。 //我这里的user是root,密码是123456(安装MySQl时自己设置的密码)。端口号port一般都是3306。 //charset字符集一定要加,不加的话,虽然连接成功,但如果含有中文,就显示不了中文内容。 MySqlConnection conn = new MySqlConnection(connectStr); conn.Open();//打开通道,建立连接 //下面主要就是条件判断 if (tbno.Text == "") { MessageBox.Show("学号不能为空"); } else if (tbname.Text == "") { MessageBox.Show("姓名不能为空"); } else if (tbno.Text != "" && tbname.Text != "") { string str = ""; if (tbdept.Text.Trim().Length == 0 && tbbirth.Text.Trim().Length == 0) { str = "insert into info(学号,姓名) values('" + tbno.Text + "','" + tbname.Text + "')";//info是数据库表名 } else if (tbdept.Text.Trim().Length == 0 && tbbirth.Text.Trim().Length > 0) { str = "insert into info(学号,姓名,出生日期) values('" + tbno.Text + "','" + tbname.Text + "','" + tbbirth.Text + "')"; } else if (tbdept.Text.Trim().Length > 0 && tbbirth.Text.Trim().Length == 0) { str = "insert into info(学号,姓名,专业) values('" + tbno.Text + "','" + tbname.Text + "','" + tbdept.Text + "')"; } else if (tbdept.Text.Trim().Length > 0 && tbbirth.Text.Trim().Length > 0) { str = "insert into info(学号,姓名,专业,出生日期) values('" + tbno.Text + "','" + tbname.Text + "','" + tbdept.Text + "','" + tbbirth.Text + "')"; } try { MySqlCommand cmd = new MySqlCommand(str, conn); cmd.ExecuteNonQuery(); } catch (MySqlException ex) { MessageBox.Show(ex.Message); } int s = 1; if (s == 1) { MessageBox.Show("成功增加一条记录"); tbno.Clear(); tbname.Clear(); tbdept.Clear(); tbbirth.Clear();//清空TextBox中的数据 } } conn.Close(); }6、点击“启动”,添加一条记录看看效果,显示“成功增加一条记录”,回到数据库表中,刷新一下,发现刚刚添加的记录已经成功保存到数据库表中了,如下图所示。 1、在FormShanChu界面中拖入控件,更改相应的属性,如下图所示。 3、代码如下: private void BtnDelete_Click(object sender, EventArgs e) { String connectStr = "server=localhost;database=studentinfosystem;user=root;password=123456;port=3306;charset=utf8"; //server=127.0.0.1或localhost。database是我的数据库名,studentinfosystem是新建的数据库名。 //我这里的user是root,密码是123456(安装MySQl时自己设置的密码)。端口号port一般都是3306。 //charset字符集一定要加,不加的话,虽然连接成功,但如果含有中文,就显示不了中文内容。 MySqlConnection conn = new MySqlConnection(connectStr); conn.Open();//打开通道,建立连接 //下面主要是条件判断 string str = ""; if (tbno.Text.Trim().Length == 0 && tbname.Text.Trim().Length == 0) { MessageBox.Show("请输入要删除的学号或姓名!"); } else if (tbno.Text.Trim().Length > 0 && tbname.Text.Trim().Length == 0) { str = "delete from info where 学号='" + tbno.Text + "'";//按“学号”删除 } else if (tbno.Text.Trim().Length == 0 && tbname.Text.Trim().Length > 0) { str = "delete from info where 姓名='" + tbname.Text + "'";//按“姓名”删除 } else if (tbno.Text.Trim().Length > 0 && tbname.Text.Trim().Length > 0) { str = "delete from info where 学号='" + tbno.Text + "'and 姓名='" + tbname.Text + "'";//按“学号+姓名”删除 } try { MySqlCommand cmd = new MySqlCommand(str, conn); cmd.ExecuteNonQuery(); } catch (MySqlException ex) { MessageBox.Show(ex.Message); } int s = 1; if (s == 1) { MessageBox.Show("删除成功"); tbno.Clear(); tbname.Clear();//清空 } conn.Close(); }4、点击“启动”,删除一条记录看看效果,显示“删除成功”,回到数据库表中,刷新一下,发现刚刚删除的记录已经成功删除,如下图所示。 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |