From 1141ec42604dbd46fc28bca04e570279af6b1b46 Mon Sep 17 00:00:00 2001 From: Dannicah Date: Sun, 29 Mar 2026 11:49:50 +0800 Subject: [PATCH 1/5] Update Form1.cs --- TestWins/Form1.cs | 68 ++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..0ad042e 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,13 +1,12 @@ -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(); @@ -16,28 +15,85 @@ public Form1() 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 069d473208be5037611a0b512b82d495df886259 Mon Sep 17 00:00:00 2001 From: Dannicah Date: Sun, 29 Mar 2026 11:50:32 +0800 Subject: [PATCH 2/5] Update ConnectionSql.cs --- TestWins/model/ConnectionSql.cs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/TestWins/model/ConnectionSql.cs b/TestWins/model/ConnectionSql.cs index 1b1bdea..9c23154 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -1,22 +1,24 @@ -namespace TestWins.Model; - -//dotnet add package MySql.Data using MySql.Data.MySqlClient; +namespace TestWins.Model; + public class ConnectionSql { private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root"; - private MySqlConnection _conn; public MySqlConnection connectSql() { - - Console.WriteLine("Connecting to DB"); - - _conn = new MySqlConnection(_connectionString); - - Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful"); - - return _conn; + try + { + var conn = new MySqlConnection(_connectionString); + conn.Open(); + Console.WriteLine("Database connection successful"); + return conn; + } + catch (Exception ex) + { + Console.WriteLine("Database connection failed: " + ex.Message); + throw; + } } -} \ No newline at end of file +} From c45c8fb9902862b36f859eaa89146656df366a18 Mon Sep 17 00:00:00 2001 From: Dannicah Date: Sun, 29 Mar 2026 11:57:30 +0800 Subject: [PATCH 3/5] Update Form1.cs --- TestWins/Form1.cs | 81 ++++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 54 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index 0ad042e..1561d1f 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,3 +1,5 @@ +using System.Drawing.Text; +using System.Xml.Linq; using TestWins.Controller; using TestWins.Model; @@ -5,6 +7,7 @@ namespace TestWins; public partial class Form1 : Form { + // business private readonly StudentController controller = new StudentController(); public Form1() @@ -15,85 +18,55 @@ public Form1() private void loadData() { - dataGridView1.DataSource = null; dataGridView1.DataSource = controller.getAll(); } private void btnAdd_Click(object sender, EventArgs e) { - try + var student = new Student { - Student s = new Student - { - Name = txtName.Text, - age = int.Parse(txtAge.Text), - course = txtCourse.Text - }; + Id = int.Parse(txtId.Text), + Name = txtName.Text, + Course = txtCourse.Text, + Year = int.Parse(txtYear.Text) + }; - controller.createStudent(s); - loadData(); - clearFields(); - } - catch (Exception ex) - { - MessageBox.Show("Error: " + ex.Message); - } + controller.create(student); + loadData(); } private void btnUpdate_Click(object sender, EventArgs e) { - try + var student = new Student { - Student s = new Student - { - studentId = txtStudentId.Text, - Name = txtName.Text, - age = int.Parse(txtAge.Text), - course = txtCourse.Text - }; + Id = int.Parse(txtId.Text), + Name = txtName.Text, + Course = txtCourse.Text, + Year = int.Parse(txtYear.Text) + }; - controller.update(s); - loadData(); - clearFields(); - } - catch (Exception ex) - { - MessageBox.Show("Update Error: " + ex.Message); - } + controller.update(student); + loadData(); } private void btnDelete_Click(object sender, EventArgs e) { - try - { - controller.delete(txtStudentId.Text); - loadData(); - clearFields(); - } - catch (Exception ex) - { - MessageBox.Show("Delete Error: " + ex.Message); - } + int id = int.Parse(txtId.Text); + + controller.delete(id); + loadData(); } private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0) { - var row = dataGridView1.Rows[e.RowIndex]; + DataGridViewRow row = dataGridView1.Rows[e.RowIndex]; - txtStudentId.Text = row.Cells["studentId"].Value.ToString(); + 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(); + txtCourse.Text = row.Cells["Course"].Value.ToString(); + txtYear.Text = row.Cells["Year"].Value.ToString(); } } - - private void clearFields() - { - txtStudentId.Text = ""; - txtName.Text = ""; - txtAge.Text = ""; - txtCourse.Text = ""; - } } From edd569e64dd50ae8a1d5172bf65b79ffcfda9065 Mon Sep 17 00:00:00 2001 From: Dannicah Date: Sun, 29 Mar 2026 11:57:59 +0800 Subject: [PATCH 4/5] Update ConnectionSql.cs --- TestWins/model/ConnectionSql.cs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/TestWins/model/ConnectionSql.cs b/TestWins/model/ConnectionSql.cs index 9c23154..bef854f 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -1,24 +1,37 @@ -using MySql.Data.MySqlClient; - 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() { + Console.WriteLine("Connecting to DB"); + + _conn = new MySqlConnection(_connectionString); + try { - var conn = new MySqlConnection(_connectionString); - conn.Open(); + _conn.Open(); Console.WriteLine("Database connection successful"); - return conn; } catch (Exception ex) { Console.WriteLine("Database connection failed: " + ex.Message); - throw; + } +return _conn; + } + + public void closeSql() + { + if (_conn != null && _conn.State == System.Data.ConnectionState.Open) + { + _conn.Close(); + Console.WriteLine("Database connection closed"); } } } From 957a44b74d29bfc0b3ab20b7f0d8428035c03b38 Mon Sep 17 00:00:00 2001 From: Dannicah Date: Sun, 29 Mar 2026 12:05:41 +0800 Subject: [PATCH 5/5] Update Form1.cs --- TestWins/Form1.cs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index 1561d1f..a475f46 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -7,19 +7,12 @@ namespace TestWins; public partial class Form1 : Form { - // business + //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) { @@ -53,10 +46,13 @@ private void btnDelete_Click(object sender, EventArgs e) { int id = int.Parse(txtId.Text); + + controller.delete(id); loadData(); } + private void dataGridView1_CellClick(object sender, EventArgs e) private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex >= 0)