net framwork winform
winform是窗体项目
该winform 项目使用的数据库是mysql,vs版本是2022
创建一个net framework项目
随便选一个框架版本
将form窗体中加入 一个DataGridView、四个button(新增、更新、删除、查询)按钮、一个textbox。这些控件在 视图---工具栏
把窗体中的button按钮名字都改掉,方便查看
在项目中创建一个student实体类
/// <summary>
/// 学号
/// </summary>
public int studentId { get; set; }
/// <summary>
/// 姓名
/// </summary>
public string sName { get; set; }
/// <summary>
/// 年龄
/// </summary>
public int sAge { get; set; }
/// <summary>
/// 爱好
/// </summary>
public string sLike { get; set; }
public Student(int studentId, string sName, int sAge, string sLike)
{
this.studentId = studentId;
this.sName = sName;
this.sAge = sAge;
this.sLike = sLike;
}
相对应mysql库中也要建一个student表,现在我们先手动创建数据库表,后期再给大家说EF映射
写语句创建库
CREATE DATABASE StudentCase
创建学生表student
CREATE TABLE student( studentid INT PRIMARY KEY, sname VARCHAR(100) , sage INT , slike VARCHAR(200) )
因为用的是mysql数据库,所以要引用mysql在net framwork 中对应的dll文件:MySql.Data
新增按钮是跳转到另外的表单上面,所以我们再创建一个 StudentManager 窗体 (表单创建4个label、4个textbox、1个button)
删改查代码
public partial class StudentManager : Form { public List<Student> students = null; public StudentManager() { InitializeComponent(); } private string connectionStr = "server=127.0.0.1;port=3306;user=root;password=root;database=studentcase";//连接数据库 private void Form1_Load(object sender, EventArgs e) { } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } /// <summary> /// 更新 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void edit_Click(object sender, EventArgs e) { //dataGridView1.DataSource.students Student student= students[dataGridView1.CurrentRow.Index]; string editStr = $"update student set studentId={dataGridView1["studentId", dataGridView1.CurrentRow.Index].Value},sName='{dataGridView1["sName", dataGridView1.CurrentRow.Index].Value}',sAge={dataGridView1["sAge", dataGridView1.CurrentRow.Index].Value},sLike='{dataGridView1["sLike",dataGridView1.CurrentRow.Index].Value}' where studentId='{student.studentId}'"; MySqlConnection mySqlConnection = new MySqlConnection(connectionStr); mySqlConnection.Open(); MySqlCommand mySqlCommand = new MySqlCommand(editStr, mySqlConnection); mySqlCommand.ExecuteNonQuery();//对连接执行SQL语句并返回行数 mySqlCommand.Dispose();//释放MySqlCommand资源 mySqlConnection.Close(); } /// <summary> /// 删除 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void delete_Click(object sender, EventArgs e) { Student student = students[dataGridView1.CurrentRow.Index]; string delStr = $"delete from student where studentId={dataGridView1["studentId", dataGridView1.CurrentRow.Index].Value}"; MySqlConnection mySqlConnection = new MySqlConnection(connectionStr); mySqlConnection.Open(); MySqlCommand mySqlCommand = new MySqlCommand(delStr,mySqlConnection); mySqlCommand.ExecuteNonQuery(); mySqlCommand.Dispose(); mySqlConnection.Close(); } /// <summary> /// 添加 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { new AddOrEdit().Show();//指定展示控件 } /// <summary> /// 查询 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void query_Click(object sender, EventArgs e) { string QueryStr = "select * from student"; MySqlConnection mySqlConnection = new MySqlConnection(connectionStr); MySqlCommand mySqlCommand = new MySqlCommand(QueryStr, mySqlConnection); mySqlConnection.Open(); MySqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader(); students = new List<Student>(); while (mySqlDataReader.Read()) { students.Add(new Student(int.Parse(mySqlDataReader[0].ToString()), mySqlDataReader[1].ToString(), int.Parse(mySqlDataReader[2].ToString()), mySqlDataReader[3].ToString())); } mySqlCommand.Dispose(); mySqlConnection.Close(); dataGridView1.DataSource = students; } }
添加代码
public partial class AddOrEdit : Form { public AddOrEdit() { InitializeComponent(); } private string connectionStr = "server=127.0.0.1;port=3306;user=root;password=root;database=studentcase";//连接数据库 private void textstudentId_TextChanged(object sender, EventArgs e) { //var reader = mySqlCommand.ExecuteReader(); } /// <summary> /// 添加 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { string addStr = $"insert into student(studentId,sName,sAge,sLike) values({int.Parse(textstudentId.Text)},'{textname.Text}',{int.Parse(textage.Text)},'{textlike.Text}');"; MySqlConnection mySqlConnection = new MySqlConnection(connectionStr); MySqlCommand mySqlCommand = new MySqlCommand(addStr, mySqlConnection); mySqlConnection.Open(); mySqlCommand.ExecuteNonQuery(); mySqlCommand.Dispose(); mySqlConnection.Close(); textstudentId.Text = "";//清空text内容 textname.Text = ""; textage.Text = ""; textlike.Text = ""; } }
本节遇到的问题
“张三“ 是四个字节一个单位进行编码的,而我们通常使用的utf-8编码在mysql数据库中默认是按照3个字节一个单位进行编码的,正是这个原因导致将数据存入mysql数据库的时候出现错误
解决方法:输入失sql语句alter table `student` convert to character set utf8mb4
内容下载地址:
链接:https://pan.baidu.com/s/1b2BMgAqBbKgjA2w--e_Beg
提取码:cudg