diff --git a/TestWins/Form1.cs b/TestWins/Form1.cs index fcefc88..a475f46 100644 --- a/TestWins/Form1.cs +++ b/TestWins/Form1.cs @@ -1,43 +1,68 @@ 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(); + } } } diff --git a/TestWins/model/ConnectionSql.cs b/TestWins/model/ConnectionSql.cs index 1b1bdea..bef854f 100644 --- a/TestWins/model/ConnectionSql.cs +++ b/TestWins/model/ConnectionSql.cs @@ -1,22 +1,37 @@ 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 +}