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 = ""; + } +} 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; + } + }