Net Core教程

【C#】实验10

本文主要是介绍【C#】实验10,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

建立项目s10,先创建MySQL数据库,并建立employee职工表,搭建数据环境。再编写一个WinForm程序,设计DataGridView数据控件来显示该表的主要字段内容,同时增加一个性别条件来检索男女职工信息。

数据库搭建:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for employee
-- ----------------------------
DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee`  (
  `sno` varchar(6) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `sname` varchar(5) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `ssex` varchar(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `sage` int(11) NULL DEFAULT NULL,
  `stitle` varchar(5) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `sdept` varchar(2) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  `sdate` datetime(0) NULL DEFAULT NULL,
  `smem` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
  PRIMARY KEY (`sno`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of employee
-- ----------------------------
INSERT INTO `employee` VALUES ('000001', '李四', '男', 27, '职员', '04', '1997-03-05 00:00:00', '爱好:摄影');
INSERT INTO `employee` VALUES ('000002', '张三', '女', 23, '职员', '04', '1998-02-06 00:00:00', '爱好:书法');
INSERT INTO `employee` VALUES ('000003', '程鑫', '男', 20, '职员', '03', '1999-01-03 00:00:00', '组织能力强,善于表现自已');
INSERT INTO `employee` VALUES ('000004', '刘红兵', '男', 25, '主管', '03', '1996-06-09 00:00:00', '组织能力强,善于交际,有上进心');
INSERT INTO `employee` VALUES ('000005', '钟舒', '女', 35, '经理', '02', '1995-08-04 00:00:00', '爱好:绘画,摄影,运动');
INSERT INTO `employee` VALUES ('000006', '江滨', '女', 30, '主管', '04', '1997-06-05 00:00:00', '有组织,有纪律,爱好:相声,小品');
INSERT INTO `employee` VALUES ('000007', '王建钢', '男', 19, '职员', '01', '2000-01-05 00:00:00', '有上进心,学习努力');
INSERT INTO `employee` VALUES ('000008', '璐娜', '女', 19, '职员', '04', '2001-02-14 00:00:00', '爱好:绘画,摄影,运动 ,有上进心');
INSERT INTO `employee` VALUES ('000009', '李小红', '女', 23, '职员', '03', '2001-03-14 00:00:00', '组织能力强,善于交际,有上进心组织能力强,善于交际,有上进心');
INSERT INTO `employee` VALUES ('000010', '梦娜', '女', 22, '职员', '02', '2001-03-14 00:00:00', '善于交际,工作能力强');
INSERT INTO `employee` VALUES ('000011', '吴大伟', '男', 24, '主管', '04', '1999-05-18 00:00:00', '工作能力强,有领导才能,有组织能力');
INSERT INTO `employee` VALUES ('000012', '李磊', '男', 26, '经理', '03', '1998-06-22 00:00:00', '工作能力强,爱好绘画,摄影,运动');
INSERT INTO `employee` VALUES ('000013', '郭薇', '女', 22, '职员', '03', '2001-07-05 00:00:00', '组织能力强,善于交际,有上进心');
INSERT INTO `employee` VALUES ('000014', '高薪', '女', 25, '职员', '02', '1999-05-28 00:00:00', '有组织,有纪律,爱好:相声,小品');
INSERT INTO `employee` VALUES ('000015', '张丽', '女', 26, '职员', '03', '1999-08-28 00:00:00', '爱好:书法');
INSERT INTO `employee` VALUES ('000016', '王民', '男', 28, '主管', '01', '1997-09-05 00:00:00', '组织能力强工作能力强,有领导才能工作能力强,有领导才能');
INSERT INTO `employee` VALUES ('000017', '李强', '男', 31, '经理', '01', '1995-03-11 00:00:00', '爱好:绘画,摄影,运动 ,有上进心');
INSERT INTO `employee` VALUES ('000018', '王经丽', '女', 28, '职员', '01', '1998-05-16 00:00:00', '组织能力强,善于交际,有上进心');
INSERT INTO `employee` VALUES ('000019', '李迪', '女', 30, '职员', '01', '1997-04-15 00:00:00', '组织能力强,善于交际,有上进心');
INSERT INTO `employee` VALUES ('000020', '王国强', '男', 18, '职员', '02', '2001-09-08 00:00:00', '上网,运动,计算机软件开发');
INSERT INTO `employee` VALUES ('000021', '李力国', '男', 20, '职员', '03', '1999-10-05 00:00:00', '善于交际,工作能力强');
INSERT INTO `employee` VALUES ('000022', '张娜', '女', 25, '主管', '02', '1998-06-18 00:00:00', '有组织,有纪律,爱好:相声,小品有组织,有纪律,爱好:相声,小品有组织,有纪律,爱好:相声,小品');
INSERT INTO `employee` VALUES ('000023', '李中青', '男', 39, '经理', '04', '1989-05-28 00:00:00', '有工作经验,工作能力强,有领导才能');
INSERT INTO `employee` VALUES ('000024', '高强', '男', 29, '职员', '03', '1995-03-05 00:00:00', '上网,运动,计算机软件开发');
INSERT INTO `employee` VALUES ('000025', '闫学成', '男', 38, '主管', '04', '1989-09-08 00:00:00', '有工作经验,工作能力强,有领导才能有工作经验,工作能力强,有领导才能');
INSERT INTO `employee` VALUES ('000026', '张汉望', '男', 55, '主管', '04', '1975-02-08 00:00:00', '爱好:书法');
INSERT INTO `employee` VALUES ('000027', '赵小青', '女', 22, '职员', '04', '1999-05-09 00:00:00', '上网,运动,计算机软件开发');
INSERT INTO `employee` VALUES ('000028', '毛丽', '女', 25, '职员', '04', '1995-08-09 00:00:00', '有组织,有纪律,爱好:相声,小品有组织,有纪律,爱好:相声,小品');
INSERT INTO `employee` VALUES ('000029', '张小婉', '女', 28, '职员', '04', '1996-09-05 00:00:00', '爱好:绘画,摄影,运动 ,有上进心');
INSERT INTO `employee` VALUES ('000030', '苏家强', '男', 30, '职员', '02', '1994-05-09 00:00:00', '爱好:书法');
INSERT INTO `employee` VALUES ('000031', '董小丽', '女', 26, '职员', '02', '1996-08-09 00:00:00', '上网,运动,计算机软件开发');
INSERT INTO `employee` VALUES ('000032', '刘力昆', '男', 45, '职员', '02', '1969-09-02 00:00:00', '爱好:书法');
INSERT INTO `employee` VALUES ('000033', '文小刚', '男', 34, '职员', '02', '1975-09-07 00:00:00', '有工作经验,工作能力强,有领导才能有工作经验,工作能力强,有领导才能');
INSERT INTO `employee` VALUES ('000034', '冯小丽', '女', 23, '职员', '02', '1996-02-08 00:00:00', '上网,运动,计算机软件开发上网,运动,计算机软件开发');
INSERT INTO `employee` VALUES ('000035', '伊能净', '女', 29, '主管', '02', '1998-05-09 00:00:00', '有工作经验,工作能力强,有领导才能有工作经验,工作能力强,有领导才能');
INSERT INTO `employee` VALUES ('000036', '尹丽', '女', 22, '职员', '04', '2000-05-06 00:00:00', '上网,运动,计算机软件开发上网,运动,计算机软件开发');
INSERT INTO `employee` VALUES ('000037', '李丽贡', '男', 35, '主管', '03', '1988-11-08 00:00:00', '有工作经验,工作能力强,有领导才能有工作经验,工作能力强,有领导才能');
INSERT INTO `employee` VALUES ('000038', '王必胜', '男', 45, '职员', '03', '1975-08-09 00:00:00', '爱好:书法');
INSERT INTO `employee` VALUES ('000039', '田丽', '女', 39, '主管', '01', '1978-12-08 00:00:00', '有工作经验,工作能力强,有领导才能有工作经验,工作能力强,有领导才能');
INSERT INTO `employee` VALUES ('000040', '周湛刚', '男', 56, '职员', '01', '1965-09-06 00:00:00', '爱好:书法');
INSERT INTO `employee` VALUES ('000041', '王牌', '男', 34, '职员', '01', '1988-09-08 00:00:00', '爱好:绘画,摄影,运动 ,有上进心');
INSERT INTO `employee` VALUES ('000042', '朱小玉', '女', 28, '职员', '01', '1995-08-08 00:00:00', '上网,运动,计算机软件开发上网,');
INSERT INTO `employee` VALUES ('000043', '陈贺', '男', 37, '职员', '01', '1992-06-05 00:00:00', '爱好:绘画,摄影,运动 ,有上进心');
INSERT INTO `employee` VALUES ('000044', '李国', '男', 34, '职员', '03', '1986-09-09 00:00:00', '有工作经验,工作能力强,');
INSERT INTO `employee` VALUES ('000045', '吴家丽', '女', 29, '职员', '04', '1998-06-08 00:00:00', '上网,运动,计算机软件开发上网,');
INSERT INTO `employee` VALUES ('000046', '赵玉', '女', 56, '职员', '03', '1978-09-02 00:00:00', '有工作经验,工作能力强,');
INSERT INTO `employee` VALUES ('000047', '白松', '男', 45, '主管', '03', '1978-09-09 00:00:00', '上网,运动,计算机软件开发上网,');
INSERT INTO `employee` VALUES ('000048', '杜丽', '女', 55, '经理', '04', '1956-09-03 00:00:00', '有工作经验,工作能力强,');
INSERT INTO `employee` VALUES ('000049', '王安强', '男', 35, '职员', '04', '1989-09-04 00:00:00', '性格开朗,爱好广泛,有组织能力');
INSERT INTO `employee` VALUES ('000050', '李素家', '女', 29, '职员', '04', '1995-09-02 00:00:00', '性格开朗,爱好广泛,有组织能力');
INSERT INTO `employee` VALUES ('000051', '张家力', '男', 39, '职员', '04', '1999-09-05 00:00:00', '组织能力强,善于表现自已');
INSERT INTO `employee` VALUES ('000052', '杨柳', '男', 45, '职员', '04', '1986-09-05 00:00:00', '性格开朗,爱好广泛,有组织能力');
INSERT INTO `employee` VALUES ('000053', '陈丽', '女', 45, '职员', '04', '1986-09-08 00:00:00', NULL);
INSERT INTO `employee` VALUES ('000054', '吕刚', '男', 34, '职员', '04', '1998-09-03 00:00:00', '性格开朗,爱好广泛,有组织能力');
INSERT INTO `employee` VALUES ('000055', '赵起刚', '男', 59, '主管', '03', '1978-09-08 00:00:00', '爱好:绘画,摄影,运动 ,有上进心');
INSERT INTO `employee` VALUES ('000056', '张四', '男', 60, '职员', '03', '1995-09-09 00:00:00', '上网,运动,计算机软件开发上网,');
INSERT INTO `employee` VALUES ('000057', '李三', '女', 58, '职员', '03', '1989-09-02 00:00:00', '爱好:摄影上网,运动,计算机软件开发上网,');
INSERT INTO `employee` VALUES ('000060', '韩丽', '女', 29, '职员', '03', '1998-08-04 00:00:00', '爱好:摄影');
INSERT INTO `employee` VALUES ('000061', '晨光', '男', 21, '职员', '03', '1999-05-08 00:00:00', '上网,运动,计算机软件开发');
INSERT INTO `employee` VALUES ('000062', '吕丽红', '女', 22, '主管', '03', '1998-08-08 00:00:00', '组织能力强,善于表现自已');
INSERT INTO `employee` VALUES ('000063', '晓丽', '女', 25, '职员', '03', '1997-01-08 00:00:00', '爱好:绘画,摄影,运动 ,有上进心');
INSERT INTO `employee` VALUES ('000064', '李小仿', '男', 29, '职员', '04', '1993-02-09 00:00:00', '性格开朗,爱好广泛,有组织能力');
INSERT INTO `employee` VALUES ('000065', '赵佛', '男', 30, '职员', '04', '1992-08-08 00:00:00', '上网,运动,计算机软件开发上网,');
INSERT INTO `employee` VALUES ('000066', '吴国', '男', 56, '职员', '04', '1969-09-04 00:00:00', '爱好:摄影');
INSERT INTO `employee` VALUES ('000067', '朱丽', '女', 34, '职员', '04', '1965-08-01 00:00:00', '组织能力强,善于表现自已');
INSERT INTO `employee` VALUES ('000068', '王小鸭', '男', 45, '职员', '04', '1975-08-09 00:00:00', '爱好:绘画,摄影,运动 ,有上进心');
INSERT INTO `employee` VALUES ('000069', '白家力', '男', 51, '职员', '04', '1965-09-02 00:00:00', '上网,运动,计算机软件开发上网,');
INSERT INTO `employee` VALUES ('000070', '高息', '男', 23, '职员', '04', '1999-05-05 00:00:00', '爱好:绘画,摄影,运动 ,有上进心');
INSERT INTO `employee` VALUES ('000071', '李国', '男', 26, '职员', '04', '1989-09-08 00:00:00', '爱好:绘画,摄影,运动 ,有上进心');
INSERT INTO `employee` VALUES ('000072', '陈四', '女', 55, '主管', '04', '1956-09-09 00:00:00', '爱好:摄影');
INSERT INTO `employee` VALUES ('000073', '吕四', '男', 45, '职员', '03', '1965-09-09 00:00:00', '性格开朗,爱好广泛,有组织能力');
INSERT INTO `employee` VALUES ('000074', '李小', '女', 32, '职员', '03', '1978-09-09 00:00:00', '爱好:绘画,摄影,运动 ,有上进心');
INSERT INTO `employee` VALUES ('000075', '张丽', '女', 22, '职员', '03', '1999-05-09 00:00:00', '性格开朗,爱好广泛,有组织能力');
INSERT INTO `employee` VALUES ('000076', '金鑫', '男', 34, '职员', '03', '1989-05-06 00:00:00', '性格开朗,爱好广泛,有组织能力');
INSERT INTO `employee` VALUES ('000077', '石磊', '男', 22, '职员', '03', '2000-08-09 00:00:00', '爱好:绘画,摄影,运动 ,有上进心');
INSERT INTO `employee` VALUES ('000078', '王森', '女', 24, '职员', '03', '2001-08-09 00:00:00', '性格开朗,爱好广泛,有组织能力');
INSERT INTO `employee` VALUES ('000079', '李晶', '女', 25, '职员', '03', '1998-05-09 00:00:00', '上网,运动,计算机软件开发上网,');
INSERT INTO `employee` VALUES ('000080', '赵品', '男', 28, '职员', '03', '1996-08-12 00:00:00', '性格开朗,爱好广泛,有组织能力');
INSERT INTO `employee` VALUES ('000081', '张柏芝', '女', 21, '职员', '04', '2002-04-20 00:00:00', '香港演员');

SET FOREIGN_KEY_CHECKS = 1;

窗体设计:

在这里插入图片描述
MainForm.cs:

/*
 * 作者:JeronZhou
 * 日期: 2021-10-28
 * 功能:使用DataGridView控件完成查询检索功能
 */

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

namespace s10
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}
		void MainFormLoad(object sender, EventArgs e)
		{
        	string Connection = "data source=localhost;database=数据库名;user id=用户名;password=密码;pooling=true;charset=utf8;";
        	using(MySqlConnection msc = new MySqlConnection(Connection))
        	{
        		msc.Open();
            	string sql = "select * from employee;";
                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 Connection = "data source=localhost;database=数据库名;user id=用户名;password=密码;pooling=true;charset=utf8;";
            MySqlConnection msc = new MySqlConnection(Connection);
            if(comboBox1.Text=="男")
            {
            	string male = "男";
            	string sql1 = string.Format("select * from employee where ssex = '{0}'", male);
            	using(MySqlDataAdapter adapter = new MySqlDataAdapter(sql1, msc))
            	{
                	DataSet ds = new DataSet();
                	adapter.Fill(ds);
                	dataGridView1.DataSource = ds.Tables[0];
            	}
            }
            else if(comboBox1.Text=="女")
            {
            	string female = "女";
            	string sql2 = string.Format("select * from employee where ssex = '{0}'", female);
            	using (MySqlDataAdapter adapter = new MySqlDataAdapter(sql2, msc))
            	{
                	DataSet ds = new DataSet();
                	adapter.Fill(ds);
                	dataGridView1.DataSource = ds.Tables[0];
            	}
            }
		}
	}
}

Program.cs:

/*
 * 作者:JeronZhou
 * 日期: 2021-10-28
 * 功能:使用DataGridView控件完成查询检索功能
 */

using System;
using System.Windows.Forms;

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

测试结果:
运行程序,显示如下界面:
在这里插入图片描述
在下方下拉框选择“男”,点击查询按钮,即可按性别查询显示结果:
在这里插入图片描述
在下方下拉框选择“女”,点击查询按钮,即可按性别查询显示结果:
在这里插入图片描述

这篇关于【C#】实验10的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!