From b2ce144c361b710351b0ee49ab0273ccecd9a3a6 Mon Sep 17 00:00:00 2001 From: Montealto Date: Sun, 29 Mar 2026 10:42:16 +0800 Subject: [PATCH 1/3] FixedConnectionSql.cs --- TestWins/FixedConnectionSql.cs | 78 ++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 TestWins/FixedConnectionSql.cs diff --git a/TestWins/FixedConnectionSql.cs b/TestWins/FixedConnectionSql.cs new file mode 100644 index 0000000..5f2af9a --- /dev/null +++ b/TestWins/FixedConnectionSql.cs @@ -0,0 +1,78 @@ +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.connectionSql(); + 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.GetInt(32), + 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 80d37585cb123b1b004a3f4d7ad2e3c566814778 Mon Sep 17 00:00:00 2001 From: Montealto Date: Sun, 29 Mar 2026 10:45:34 +0800 Subject: [PATCH 2/3] Form1.cs --- TestWins/Form1.cs | 83 ++++++++++++++++++++++++++++++++++------------- 1 file changed, 61 insertions(+), 22 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..180c3a3 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,43 +1,82 @@ -using System.Drawing.Text; -using TestWins.Controller; - -namespace TestWins; - -public partial class Form1 : Form -{ - //business - - private readonly StudentController controller = new StudentController(); - public Form1() - { - InitializeComponent(); - loadData(); - } - - private void loadData() - { - dataGridView1.DataSource = controller.getAll(); - } - - private void btnAdd_Click(object sender, EventArgs e) +private void btnAdd_Click(object sender, EventArgs e) { + try + { + var student = new Student + { + Name = txtName.Text, + Age = int.Parse(txtAge.Text) + }; + controller.add(student); + loadData(); + ClearFields(); + MessageBox.Show("Student added successfully!"); + } + catch(Exception e) + { + MessageBox.Show("Error: " + ex.Message); + } } private void btnUpdate_Click(object sender, EventArgs e) { + try + { + var student = new Student + { + id = int.Parse(txtId.Text), + Name = txtName.Text, + Age = int.Parse(txtAge.Text) + }; + controller.update(student); + loadData(); + ClearFields(); + MessageBox.Show("Student updated successfully!"); + } + catch(Exception e) + { + MessageBox.Show("Erro: " + 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 e) + { + MessageBox.Show("Error: " + ex.Message); + } } private void dataGridView1_CellClick(object sender, EventArgs e) { + if(e.RowIndex >= 0) + { + DataGridViewRow row = dataGridView1.Rows[e.RowIndex]; + txtId.Text = row.Cells["Id"].Value.ToString(); + txtName.Text = row.Cells["Name"].Value.ToString(); + txtAge.Text = row.Cells["Age"].Value.ToString(); + } } } From 8c608c2ef4dc4fffb87d24c7adf3e2f3df8f634a Mon Sep 17 00:00:00 2001 From: Montealto Date: Sun, 29 Mar 2026 10:48:20 +0800 Subject: [PATCH 3/3] Form1.cs --- TestWins/Form1.cs | 99 ++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 58 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index 180c3a3..75318da 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,82 +1,65 @@ -private void btnAdd_Click(object sender, EventArgs e) +using System.Drawing.Text; +using TestWins.Controller; + +namespace TestWins; + +public partial class Form1 : Form +{ + //business + + private readonly StudentController controller = new StudentController(); + public Form1() { - try - { - var student = new Student - { - Name = txtName.Text, - Age = int.Parse(txtAge.Text) - }; - controller.add(student); - loadData(); - ClearFields(); + InitializeComponent(); + loadData(); + } - MessageBox.Show("Student added successfully!"); - } - catch(Exception e) - { - MessageBox.Show("Error: " + ex.Message); - } + private void loadData() + { + dataGridView1.DataSource = controller.getAll(); } - private void btnUpdate_Click(object sender, EventArgs e) + private void btnAdd_Click(object sender, EventArgs e) { - try + Students s = new Students() { - var student = new Student - { - id = int.Parse(txtId.Text), - Name = txtName.Text, - Age = int.Parse(txtAge.Text) - }; - controller.update(student); - loadData(); - ClearFields(); + Name = txtName.Text, + Age = int.Parse(txtAge.Text), + Course = txtCourse.Text + }; + controller.insert(s); + loadData(); + } - MessageBox.Show("Student updated successfully!"); - } - catch(Exception e) + private void btnUpdate_Click(object sender, EventArgs e) + { + Students S = new Students() { - MessageBox.Show("Erro: " + ex.Message); - } + Id = int.Parse(txtId.Text), + Name = txtName.Text, + Age = int.Parse(txtAge.Text), + Course = txtCourse.Text + }; + controller.update(S); + loadData(); } 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 e) - { - MessageBox.Show("Error: " + ex.Message); - } + int id = int.Parse(txtId.Text); + controller.delete(id); + loadData(); } private void dataGridView1_CellClick(object sender, EventArgs e) { - if(e.RowIndex >= 0) + if (e.RowIndex >= 0) { DataGridViewRow row = dataGridView1.Rows[e.RowIndex]; - txtId.Text = row.Cells["Id"].Value.ToString(); txtName.Text = row.Cells["Name"].Value.ToString(); txtAge.Text = row.Cells["Age"].Value.ToString(); + txtCourse.Text = row.Cells["Course"].Value.ToString();s } } }