From 014b435e2f1397f54ae6a0180d4764922e12a43c Mon Sep 17 00:00:00 2001 From: sleepysevi Date: Sun, 29 Mar 2026 18:26:53 +0800 Subject: [PATCH 1/2] Create FixedConnectionSql.cs --- TestWins/model/FixedConnectionSql.cs | 66 ++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 TestWins/model/FixedConnectionSql.cs diff --git a/TestWins/model/FixedConnectionSql.cs b/TestWins/model/FixedConnectionSql.cs new file mode 100644 index 0000000..5267f4a --- /dev/null +++ b/TestWins/model/FixedConnectionSql.cs @@ -0,0 +1,66 @@ +using MySql.Data.MySqlClient; +using TestWins.Model; + +namespace TestWins.Repository +{ + public class StudentRepository + { + private readonly FixedConnectionSql _db = new FixedConnectionSql(); + + public void Create(Student student) + { + using var conn = _db.connectSql(); + conn.Open(); + string query = "INSERT INTO students (name, age, course) VALUES (@name, @age, @course)"; + using var cmd = new MySqlCommand(query, conn); + cmd.Parameters.AddWithValue("@name", student.Name); + cmd.Parameters.AddWithValue("@age", student.Age); + cmd.Parameters.AddWithValue("@course", student.Course); + cmd.ExecuteNonQuery(); + } + + public List GetAll() + { + var list = new List(); + using var conn = _db.connectSql(); + conn.Open(); + string query = "SELECT * FROM students"; + using var cmd = new MySqlCommand(query, conn); + using var reader = cmd.ExecuteReader(); + while (reader.Read()) + { + list.Add(new Student + { + StudentId = reader.GetInt32(0), + Name = reader.GetString(1), + Age = reader.GetInt32(2), + Course = reader.GetString(3), + }); + } + return list; + } + + public void Update(Student student) + { + using var conn = _db.connectSql(); + conn.Open(); + string query = "UPDATE students SET name = @name, age = @age, course = @course WHERE id = @id"; + using var cmd = new MySqlCommand(query, conn); + cmd.Parameters.AddWithValue("@id", student.StudentId); + cmd.Parameters.AddWithValue("@name", student.Name); + cmd.Parameters.AddWithValue("@age", student.Age); + cmd.Parameters.AddWithValue("@course", student.Course); + cmd.ExecuteNonQuery(); + } + + public void Delete(int id) + { + using var conn = _db.connectSql(); + conn.Open(); + string query = "DELETE FROM students WHERE id = @id"; + using var cmd = new MySqlCommand(query, conn); + cmd.Parameters.AddWithValue("@id", id); + cmd.ExecuteNonQuery(); + } + } +} From dd7b31cc2abd0a947bb72b7820535a795cb30939 Mon Sep 17 00:00:00 2001 From: sleepysevi Date: Sun, 29 Mar 2026 18:28:15 +0800 Subject: [PATCH 2/2] Fix and update Form1.cs --- TestWins/Form1.cs | 89 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 77 insertions(+), 12 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..b14cce5 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,43 +1,108 @@ -using System.Drawing.Text; using TestWins.Controller; +using TestWins.Model; namespace TestWins; public partial class Form1 : Form { - //business - private readonly StudentController controller = new StudentController(); + public Form1() { InitializeComponent(); - loadData(); + LoadData(); } - private void loadData() + private void LoadData() { - dataGridView1.DataSource = controller.getAll(); + dataGridView1.DataSource = controller.GetAll(); } private void btnAdd_Click(object sender, EventArgs e) { - + try + { + var student = new Student + { + Name = txtName.Text, + Age = int.Parse(txtAge.Text), + Course = txtCourse.Text + }; + controller.Add(student); + LoadData(); + ClearFields(); + MessageBox.Show("Student added successfully!"); + } + catch (Exception ex) + { + MessageBox.Show("Error: " + ex.Message); + } } private void btnUpdate_Click(object sender, EventArgs e) { - + try + { + var student = new Student + { + StudentId = int.Parse(txtId.Text), + Name = txtName.Text, + Age = int.Parse(txtAge.Text), + Course = txtCourse.Text + }; + controller.Update(student); + LoadData(); + ClearFields(); + MessageBox.Show("Student updated successfully!"); + } + catch (Exception ex) + { + MessageBox.Show("Error: " + ex.Message); + } } private void btnDelete_Click(object sender, EventArgs e) { - - - + try + { + int id = int.Parse(txtId.Text); + var confirm = MessageBox.Show( + "Are you sure you want to delete this student?", + "Confirm Delete", + MessageBoxButtons.YesNo + ); + if (confirm == DialogResult.Yes) + { + controller.Delete(id); + LoadData(); + ClearFields(); + MessageBox.Show("Student deleted successfully!"); + } + } + catch (Exception ex) + { + MessageBox.Show("Error: " + ex.Message); + } } - private void dataGridView1_CellClick(object sender, EventArgs e) + + private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { + if (e.RowIndex >= 0) + { + DataGridViewRow row = dataGridView1.Rows[e.RowIndex]; + txtId.Text = row.Cells["StudentId"].Value.ToString(); + txtName.Text = row.Cells["Name"].Value.ToString(); + txtAge.Text = row.Cells["Age"].Value.ToString(); + txtCourse.Text = row.Cells["Course"].Value.ToString(); + } + } + private void ClearFields() + { + txtId.Text = string.Empty; + txtName.Text = string.Empty; + txtAge.Text = string.Empty; + txtCourse.Text = string.Empty; } }