From 374c4233ed6fdb257383096cb1a37d9ef7b13e4d Mon Sep 17 00:00:00 2001 From: Broniolaiz Date: Sun, 29 Mar 2026 12:11:17 +0800 Subject: [PATCH 1/2] Implement CRUD operations for Student management --- TestWins/Form1.cs | 64 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..b25c740 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,5 +1,6 @@ using System.Drawing.Text; using TestWins.Controller; +using TestWins.Model; namespace TestWins; @@ -8,36 +9,95 @@ public partial class Form1 : Form //business private readonly StudentController controller = new StudentController(); + public Form1() { InitializeComponent(); - loadData(); - } private void loadData() { + dataGridView1.DataSource = null; dataGridView1.DataSource = controller.getAll(); } private void btnAdd_Click(object sender, EventArgs e) { + try + { + Student s = new Student + { + Name = txtName.Text, + age = int.Parse(txtAge.Text), + course = txtCourse.Text + }; + controller.createStudent(s); + loadData(); + clearFields(); + } + catch (Exception ex) + { + MessageBox.Show("Error: " + ex.Message); + } } private void btnUpdate_Click(object sender, EventArgs e) { + try + { + Student s = new Student + { + studentId = txtStudentId.Text, + Name = txtName.Text, + age = int.Parse(txtAge.Text), + course = txtCourse.Text + }; + controller.update(s); + loadData(); + clearFields(); + } + catch (Exception ex) + { + MessageBox.Show("Update Error: " + ex.Message); + } } private void btnDelete_Click(object sender, EventArgs e) { + try + { + controller.delete(txtStudentId.Text); + loadData(); + clearFields(); + } + catch (Exception ex) + { + MessageBox.Show("Delete Error: " + ex.Message); + } + } + private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) + { + if (e.RowIndex >= 0) + { + var row = dataGridView1.Rows[e.RowIndex]; + txtStudentId.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 dataGridView1_CellClick(object sender, EventArgs e) + private void clearFields() { + txtStudentId.Text = ""; + txtName.Text = ""; + txtAge.Text = ""; + txtCourse.Text = ""; } } From c4998e28862e57cc014d8ef5b2ce272cba9376c8 Mon Sep 17 00:00:00 2001 From: Broniolaiz Date: Sun, 29 Mar 2026 12:18:54 +0800 Subject: [PATCH 2/2] Refactor ConnectionSql class for database connection --- TestWins/model/ConnectionSql.cs | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/TestWins/model/ConnectionSql.cs b/TestWins/model/ConnectionSql.cs index 1b1bdea..4b486c7 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -1,22 +1,29 @@ -namespace TestWins.Model; - -//dotnet add package MySql.Data using MySql.Data.MySqlClient; -public class ConnectionSql +namespace TestWins.Model { - private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root"; - private MySqlConnection _conn; - - public MySqlConnection connectSql() + public class ConnectionSql { + private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root"; - Console.WriteLine("Connecting to DB"); + public MySqlConnection connectSql() + { + try + { + Console.WriteLine("Connecting to DB"); - _conn = new MySqlConnection(_connectionString); + MySqlConnection conn = new MySqlConnection(_connectionString); + conn.Open(); - Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful"); + Console.WriteLine("Database connection successful"); - return _conn; + return conn; + } + catch (Exception ex) + { + Console.WriteLine("Database connection failed: " + ex.Message); + throw; + } + } } -} \ No newline at end of file +}