diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..826bf5e 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,5 +1,7 @@ using System.Drawing.Text; +using System.Xml.Linq; using TestWins.Controller; +using TestWins.Model; namespace TestWins; @@ -8,6 +10,7 @@ public partial class Form1 : Form //business private readonly StudentController controller = new StudentController(); + public Form1() { InitializeComponent(); @@ -21,23 +24,53 @@ private void loadData() 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(); + } } } diff --git a/TestWins/model/ConnectionSql.cs b/TestWins/model/ConnectionSql.cs index 1b1bdea..cabc2b1 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -1,12 +1,14 @@ 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() { @@ -16,7 +18,14 @@ public MySqlConnection connectSql() _conn = new MySqlConnection(_connectionString); Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful"); - - return _conn; + try + { + _conn.Open(); + Console.WriteLine("Database connection successful"); + } + catch (Exception ex) + { + Console.WriteLine("Database connection failed: " + ex.Message); + } +return _conn; } -} \ No newline at end of file