From ad335019aee52620f727340c4b9661727913907f Mon Sep 17 00:00:00 2001 From: zero123-max Date: Mon, 30 Mar 2026 10:03:50 +0800 Subject: [PATCH 1/2] Form1.cs --- TestWins/Form1.cs | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..e06a00a 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,43 +1,67 @@ using System.Drawing.Text; +using System.Xml.Linq; using TestWins.Controller; +using TestWins.Model; 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) { + var student = new Student + { + Id = int.Parse(txtId.Text), + Name = txtName.Text, + Course = txtCourse.Text, + Year = int.Parse(txtYear.Text) + }; + controller.create(student); + loadData(); } private void btnUpdate_Click(object sender, EventArgs e) { + var student = new Student + { + Id = int.Parse(txtId.Text), + Name = txtName.Text, + Course = txtCourse.Text, + Year = int.Parse(txtYear.Text) + }; + controller.update(student); + loadData(); } 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) + { + 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(); + txtYear.Text = row.Cells["Year"].Value.ToString(); + } } } From fe2c8ce9347a707f40c1e7f95c137ade496070e3 Mon Sep 17 00:00:00 2001 From: zero123-max Date: Mon, 30 Mar 2026 10:04:59 +0800 Subject: [PATCH 2/2] ConnectionSql.cs Refactor database connection handling with error catching and closing logic. --- TestWins/model/ConnectionSql.cs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/TestWins/model/ConnectionSql.cs b/TestWins/model/ConnectionSql.cs index 1b1bdea..8c6a55c 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -1,22 +1,41 @@ namespace TestWins.Model; //dotnet add package MySql.Data +// 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; + 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"); + try + { + _conn.Open(); + Console.WriteLine("Database connection successful"); + } + catch (Exception ex) + { + Console.WriteLine("Database connection failed: " + ex.Message); + } +return _conn; + } return _conn; + public void closeSql() + { + if (_conn != null && _conn.State == System.Data.ConnectionState.Open) + { + _conn.Close(); + Console.WriteLine("Database connection closed"); + } } -} \ No newline at end of file +} +} +}