From 299242aa19f2630c640f613afcadfd6b1252e4be Mon Sep 17 00:00:00 2001 From: GlennPhilip1607 Date: Sun, 29 Mar 2026 21:11:31 +0800 Subject: [PATCH 1/2] Refactor ConnectionSql class and method names --- TestWins/model/ConnectionSql.cs | 34 +++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/TestWins/model/ConnectionSql.cs b/TestWins/model/ConnectionSql.cs index 1b1bdea..3879d34 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -1,22 +1,28 @@ -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"; + private MySqlConnection _conn; - Console.WriteLine("Connecting to DB"); - - _conn = new MySqlConnection(_connectionString); + public MySqlConnection ConnectSql() + { + try + { + Console.WriteLine("Connecting to DB..."); - Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful"); + _conn = new MySqlConnection(_connectionString); + _conn.Open(); // + Console.WriteLine("Database connection successful"); + } + catch (Exception ex) + { + Console.WriteLine("Database connection failed: " + ex.Message); + } - return _conn; + return _conn; + } } -} \ No newline at end of file +} From 8f1fe16d743f09e8bc22e67f372fd66a895fa2bd Mon Sep 17 00:00:00 2001 From: GlennPhilip1607 Date: Sun, 29 Mar 2026 21:14:19 +0800 Subject: [PATCH 2/2] Add CRUD operations for student records in Form1 Implemented add, update, delete, and clear functionality for student records in Form1. --- TestWins/Form1.cs | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..6cdab04 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,13 +1,11 @@ -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 +19,46 @@ private void loadData() private void btnAdd_Click(object sender, EventArgs e) { - + controller.add(txtName.Text, txtCourse.Text); + loadData(); + clearFields(); } private void btnUpdate_Click(object sender, EventArgs e) { - + int id = int.Parse(txtId.Text); + controller.update(id, txtName.Text, txtCourse.Text); + loadData(); + clearFields(); } + private void btnDelete_Click(object sender, EventArgs e) { + int id = int.Parse(txtId.Text); + controller.delete(id); + loadData(); + clearFields(); + } + + private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs 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(); + txtCourse.Text = row.Cells["course"].Value.ToString(); + } } - private void dataGridView1_CellClick(object sender, EventArgs e) + + private void clearFields() { - + txtId.Clear(); + txtName.Clear(); + txtCourse.Clear(); } }