From bc9cf30b8383290c5ad67a931c151989d2a4adce Mon Sep 17 00:00:00 2001 From: johnferrari-oss Date: Sun, 29 Mar 2026 17:16:42 +0800 Subject: [PATCH 1/3] FixedConnectionSql.cs --- FixedConnectionSql.cs | 78 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 FixedConnectionSql.cs diff --git a/FixedConnectionSql.cs b/FixedConnectionSql.cs new file mode 100644 index 0000000..5f2af9a --- /dev/null +++ b/FixedConnectionSql.cs @@ -0,0 +1,78 @@ +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 eed5bb69e4c7e0f7852bdbad48834ea22c71a313 Mon Sep 17 00:00:00 2001 From: johnferrari-oss Date: Sun, 29 Mar 2026 17:17:49 +0800 Subject: [PATCH 2/3] Form1.cs --- Form1.cs | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 Form1.cs diff --git a/Form1.cs b/Form1.cs new file mode 100644 index 0000000..bc352c5 --- /dev/null +++ b/Form1.cs @@ -0,0 +1,64 @@ +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) + { + Students s = new Students() + { + Name = txtName.Text, + Age = int.Parse(txtAge.Text), + Course = txtCourse.Text + }; + controller.insert(s); + loadData(); + } + + private void btnUpdate_Click(object sender, EventArgs e) + { + Students S = new Students() + { + Id = int.Parse(txtId.Text), + Name = txtName.Text, + Age = int.Parse(txtAge.Text), + Course = txtCourse.Text + }; + controller.update(S); + 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) + { + 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(); + txtCourse.Text = row.Cells["Course"].Value.ToString();s + } + } +} From 7789577674263988889d1f1e3fe123aa519633e2 Mon Sep 17 00:00:00 2001 From: johnferrari-oss Date: Sun, 29 Mar 2026 17:19:16 +0800 Subject: [PATCH 3/3] ConnectionSql.cs --- ConnectionSql.cs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 ConnectionSql.cs diff --git a/ConnectionSql.cs b/ConnectionSql.cs new file mode 100644 index 0000000..dc4c3fa --- /dev/null +++ b/ConnectionSql.cs @@ -0,0 +1,44 @@ +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() + { + return new MySqlConnection(_connectionString); + + } + public MySqlConnection GetOpenConnection() + { + try + { + var conn = new MySqlConnection(_connectionString); + conn.Open(); + Console.WriteLine("Database connection successful"); + return conn; + } + catch (MySqlException ex) + { + Console.WriteLine($"Database connection failed: {ex.Message}"); + return null; + } + catch (Exception ex) + { + Console.WriteLine($"An unexpected error occurred: {ex.Message}"); + return null; + } + } + Console.WriteLine("Connecting to DB"); + + _conn = new MySqlConnection(_connectionString); + + Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful"); + + return _conn; + } +}