From 2cc1977fb36efb09726ad6818e78b02f08017ba7 Mon Sep 17 00:00:00 2001 From: Rey Date: Sun, 29 Mar 2026 10:31:59 +0800 Subject: [PATCH 1/3] Refactor ConnectionSql class for improved clarity --- TestWins/model/ConnectionSql.cs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/TestWins/model/ConnectionSql.cs b/TestWins/model/ConnectionSql.cs index 1b1bdea..ab29d1c 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -2,21 +2,24 @@ namespace TestWins.Model; //dotnet add package MySql.Data using MySql.Data.MySqlClient; - -public class ConnectionSql { - 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;"; + private MySqlConnection _conn; - Console.WriteLine("Connecting to DB"); + public MySqlConnection connectSql() + { + Console.WriteLine("Connecting to DB"); - _conn = new MySqlConnection(_connectionString); + _conn = new MySqlConnection(_connectionString); + _conn.Open(); - Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful"); + Console.WriteLine(_conn.State == System.Data.ConnectionState.Open + ? "Database connection successful" + : "Database Connection Failed"); - return _conn; + return _conn; + } } -} \ No newline at end of file +} From 4416d30b37111d1765c4898f493c952e2ae44212 Mon Sep 17 00:00:00 2001 From: Rey Date: Sun, 29 Mar 2026 11:32:38 +0800 Subject: [PATCH 2/3] Fix formatting issues in ConnectionSql.cs --- TestWins/model/ConnectionSql.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/TestWins/model/ConnectionSql.cs b/TestWins/model/ConnectionSql.cs index ab29d1c..03c7b7a 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -2,7 +2,7 @@ namespace TestWins.Model; //dotnet add package MySql.Data using MySql.Data.MySqlClient; -{ + public class ConnectionSql { private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root;"; @@ -22,4 +22,4 @@ public MySqlConnection connectSql() return _conn; } } -} + From 84e5e7f2e89fbc1974d2d22991d966427adbe6b2 Mon Sep 17 00:00:00 2001 From: Rey Date: Sun, 29 Mar 2026 11:35:18 +0800 Subject: [PATCH 3/3] Implement student management features in Form1 Added functionality to create, update, and delete students. Implemented clearFields method to reset input fields. --- TestWins/Form1.cs | 48 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 9 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..9d9ce41 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,13 +1,10 @@ using System.Drawing.Text; using TestWins.Controller; - namespace TestWins; - public partial class Form1 : Form { - //business - private readonly StudentController controller = new StudentController(); + public Form1() { InitializeComponent(); @@ -21,23 +18,56 @@ private void loadData() private void btnAdd_Click(object sender, EventArgs e) { - + var student = new TestWins.Model.Student + { + studentId = txtStudentId.Text, + Name = txtName.Text, + age = int.Parse(txtAge.Text), + course = txtCourse.Text + }; + controller.createStudent(student); + loadData(); + clearFields(); } private void btnUpdate_Click(object sender, EventArgs e) { - + var student = new TestWins.Model.Student + { + studentId = txtStudentId.Text, + Name = txtName.Text, + age = int.Parse(txtAge.Text), + course = txtCourse.Text + }; + controller.update(student); + loadData(); + clearFields(); } private void btnDelete_Click(object sender, EventArgs e) { - - - + controller.delete(txtStudentId.Text); + loadData(); + clearFields(); } private void dataGridView1_CellClick(object sender, EventArgs e) { + if (dataGridView1.CurrentRow != null) + { + DataGridViewRow row = dataGridView1.CurrentRow; + txtStudentId.Text = row.Cells[0].Value.ToString(); + txtName.Text = row.Cells[1].Value.ToString(); + txtAge.Text = row.Cells[2].Value.ToString(); + txtCourse.Text = row.Cells[3].Value.ToString(); + } + } + private void clearFields() + { + txtStudentId.Text = ""; + txtName.Text = ""; + txtAge.Text = ""; + txtCourse.Text = ""; } }