From 9971df17228286a937fb013d7505e2af7731f835 Mon Sep 17 00:00:00 2001 From: sha_badilles Date: Sun, 29 Mar 2026 16:17:22 +0800 Subject: [PATCH 1/6] Create Form1.cs --- Form1.cs | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 Form1.cs diff --git a/Form1.cs b/Form1.cs new file mode 100644 index 0000000..a49bb4b --- /dev/null +++ b/Form1.cs @@ -0,0 +1,72 @@ +private void btnAdd_Click(object sender, EventArgs e) +{ +try +{ +var student = new Student +{ +Name = txtName.Text, +Age = int.Parse(txtAge.Text) +}; +controller.add(student); +loadData(); +ClearFields(); +MessageBox.Show("Student added successfully!"); +} +catch(Exception e) +{ +MessageBox.Show("Error: " + ex.Message); +} +} +private void btnUpdate_Click(object sender, EventArgs e) +{ +try +{ +var student = new Student +{ +id = int.Parse(txtId.Text), +Name = txtName.Text, +Age = int.Parse(txtAge.Text) +}; +controller.update(student); +loadData(); +ClearFields(); +MessageBox.Show("Student updated successfully!"); +} +catch(Exception e) +{ +MessageBox.Show("Erro: " + ex.Message); +} +} +private void btnDelete_Click(object sender, EventArgs e) +{ +try +{ +int id = int.Parse(txtId.Text); +var confirm = MessageBox.Show( +"Are you sure you want to delete this student?", +"Confirm Delete", +MessageBoxButtons.YesNo +); +if(confirm == DialogResult.Yes) +{ +controller.delete(id); +loadData(); +ClearFields(); +MessageBox.Show("Student deleted successfully!"); +} +} +catch(Exception e) +{ +MessageBox.Show("Error: " + ex.Message); +} +} +private void dataGridView1_CellClick(object sender, EventArgs 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(); +txtAge.Text = row.Cells["Age"].Value.ToString(); +} +} From bbb05cd937292d42a3b477893981c30cdfbf32f4 Mon Sep 17 00:00:00 2001 From: sha_badilles Date: Sun, 29 Mar 2026 16:18:42 +0800 Subject: [PATCH 2/6] Create FixedConnectionSql.cs --- FixedConnectionSql.cs | 62 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 FixedConnectionSql.cs diff --git a/FixedConnectionSql.cs b/FixedConnectionSql.cs new file mode 100644 index 0000000..ca89b19 --- /dev/null +++ b/FixedConnectionSql.cs @@ -0,0 +1,62 @@ +using MySql.Data.MySqlClient; +using TestWins.Model; +namespace TestWins.Repository +{ +public class StudentRepository +{ +private readonly FixedConnectionSql_db = new FixedConnectionSql(); +public void Create(Student student) +{ +using var conn = _db.connectSql(); +conn.Open(); +string query = "INSERT INTO students (name, age, course) VALUES (@name, @age, +@course)"; +using var cmd = new MySqlCommand(query, conn); +cmd.Parameters.AddWithValue("@name", student.Name); +cmd.Parameters.AddWithValue("@age", student.age); +cmd.Parameters.AddWithValue("@course", student.course); +cmd.ExecuteNonQuery(); +} +public List GetAll() +{ +var list = new List(); +using var conn = _db.connectionSql(); +conn.Open(); +string query = "SELECT * FROM students"; +using var cmd = new MySqlCommand(query, conn); +using var reader = cmd.ExecuteReader(); +while(reader.Read()) +{ +list.Add(new Student +{ +studentId = reader.GetInt32(0), +Name = reader.GetString(1), +age = reader.GetInt(32), +course = reader.GetString(3), +}); +} +return list; +} +public void Update(Student student) +{ +using var conn = _db.connectSql(); +conn.Open(); +string query = "UPDATE students SET name = @name, age = @age, course = @course +WHERE id = @id"; +using var cmd = new MySqlCommand(query, conn); +cmd.Parameters.AddWithValue("@id", student.studentId); +cmd.Parameters.AddWithValue("@name", student.Name); +cmd.Parameters.AddwithValue("@age", student.age); +cmd.Parameters.AddWithValue("@course", student.course); +cmd.ExecuteNonQuery(); +} +public void Delete(int id) +{ +using var conn = _db.connectSql(); +conn.Open(); +string query = "DELETE FROM students WHERE id = @id"; +using var cmd = new MySqlCommand(query, conn); +cmd.Parameters.AddWithValue("@id", id); +cmd.ExecuteNonQuery(); +} +} From 82cd643d4a7a08fd3668ea9f326a99387cb75612 Mon Sep 17 00:00:00 2001 From: sha_badilles Date: Sun, 29 Mar 2026 16:19:32 +0800 Subject: [PATCH 3/6] Delete FixedConnectionSql.cs --- FixedConnectionSql.cs | 62 ------------------------------------------- 1 file changed, 62 deletions(-) delete mode 100644 FixedConnectionSql.cs diff --git a/FixedConnectionSql.cs b/FixedConnectionSql.cs deleted file mode 100644 index ca89b19..0000000 --- a/FixedConnectionSql.cs +++ /dev/null @@ -1,62 +0,0 @@ -using MySql.Data.MySqlClient; -using TestWins.Model; -namespace TestWins.Repository -{ -public class StudentRepository -{ -private readonly FixedConnectionSql_db = new FixedConnectionSql(); -public void Create(Student student) -{ -using var conn = _db.connectSql(); -conn.Open(); -string query = "INSERT INTO students (name, age, course) VALUES (@name, @age, -@course)"; -using var cmd = new MySqlCommand(query, conn); -cmd.Parameters.AddWithValue("@name", student.Name); -cmd.Parameters.AddWithValue("@age", student.age); -cmd.Parameters.AddWithValue("@course", student.course); -cmd.ExecuteNonQuery(); -} -public List GetAll() -{ -var list = new List(); -using var conn = _db.connectionSql(); -conn.Open(); -string query = "SELECT * FROM students"; -using var cmd = new MySqlCommand(query, conn); -using var reader = cmd.ExecuteReader(); -while(reader.Read()) -{ -list.Add(new Student -{ -studentId = reader.GetInt32(0), -Name = reader.GetString(1), -age = reader.GetInt(32), -course = reader.GetString(3), -}); -} -return list; -} -public void Update(Student student) -{ -using var conn = _db.connectSql(); -conn.Open(); -string query = "UPDATE students SET name = @name, age = @age, course = @course -WHERE id = @id"; -using var cmd = new MySqlCommand(query, conn); -cmd.Parameters.AddWithValue("@id", student.studentId); -cmd.Parameters.AddWithValue("@name", student.Name); -cmd.Parameters.AddwithValue("@age", student.age); -cmd.Parameters.AddWithValue("@course", student.course); -cmd.ExecuteNonQuery(); -} -public void Delete(int id) -{ -using var conn = _db.connectSql(); -conn.Open(); -string query = "DELETE FROM students WHERE id = @id"; -using var cmd = new MySqlCommand(query, conn); -cmd.Parameters.AddWithValue("@id", id); -cmd.ExecuteNonQuery(); -} -} From 4c509b009e034cc46451dfa382430ec5b98c0ca2 Mon Sep 17 00:00:00 2001 From: sha_badilles Date: Sun, 29 Mar 2026 16:20:05 +0800 Subject: [PATCH 4/6] Delete Form1.cs --- Form1.cs | 72 -------------------------------------------------------- 1 file changed, 72 deletions(-) delete mode 100644 Form1.cs diff --git a/Form1.cs b/Form1.cs deleted file mode 100644 index a49bb4b..0000000 --- a/Form1.cs +++ /dev/null @@ -1,72 +0,0 @@ -private void btnAdd_Click(object sender, EventArgs e) -{ -try -{ -var student = new Student -{ -Name = txtName.Text, -Age = int.Parse(txtAge.Text) -}; -controller.add(student); -loadData(); -ClearFields(); -MessageBox.Show("Student added successfully!"); -} -catch(Exception e) -{ -MessageBox.Show("Error: " + ex.Message); -} -} -private void btnUpdate_Click(object sender, EventArgs e) -{ -try -{ -var student = new Student -{ -id = int.Parse(txtId.Text), -Name = txtName.Text, -Age = int.Parse(txtAge.Text) -}; -controller.update(student); -loadData(); -ClearFields(); -MessageBox.Show("Student updated successfully!"); -} -catch(Exception e) -{ -MessageBox.Show("Erro: " + ex.Message); -} -} -private void btnDelete_Click(object sender, EventArgs e) -{ -try -{ -int id = int.Parse(txtId.Text); -var confirm = MessageBox.Show( -"Are you sure you want to delete this student?", -"Confirm Delete", -MessageBoxButtons.YesNo -); -if(confirm == DialogResult.Yes) -{ -controller.delete(id); -loadData(); -ClearFields(); -MessageBox.Show("Student deleted successfully!"); -} -} -catch(Exception e) -{ -MessageBox.Show("Error: " + ex.Message); -} -} -private void dataGridView1_CellClick(object sender, EventArgs 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(); -txtAge.Text = row.Cells["Age"].Value.ToString(); -} -} From 2e23337ef86e44ae3bf027c5fe78a50e8a76af68 Mon Sep 17 00:00:00 2001 From: sha_badilles Date: Sun, 29 Mar 2026 22:48:25 +0800 Subject: [PATCH 5/6] Create UpdateForm1.cs --- TestWins/UpdateForm1.cs | 73 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 TestWins/UpdateForm1.cs diff --git a/TestWins/UpdateForm1.cs b/TestWins/UpdateForm1.cs new file mode 100644 index 0000000..ff548d8 --- /dev/null +++ b/TestWins/UpdateForm1.cs @@ -0,0 +1,73 @@ +using System.Drawing.Text; +using TestWins.Controller; +namespace TestWins; +public partial class Form1 : Form +{ + 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 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 = ""; + } +} From 0891c0d1487d2228d8b5ff2f414cd997b650a52b Mon Sep 17 00:00:00 2001 From: sha_badilles Date: Sun, 29 Mar 2026 22:51:24 +0800 Subject: [PATCH 6/6] Add ConnectionSql class for MySQL database connection --- TestWins/model/UpdateConnectionSql.cs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 TestWins/model/UpdateConnectionSql.cs diff --git a/TestWins/model/UpdateConnectionSql.cs b/TestWins/model/UpdateConnectionSql.cs new file mode 100644 index 0000000..58b66d8 --- /dev/null +++ b/TestWins/model/UpdateConnectionSql.cs @@ -0,0 +1,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() + { + Console.WriteLine("Connecting to DB"); + + _conn = new MySqlConnection(_connectionString); + _conn.Open(); + + Console.WriteLine(_conn.State == System.Data.ConnectionState.Open + ? "Database connection successful" + : "Database Connection Failed"); + + return _conn; + } + }