diff --git a/Forms1.cs b/Forms1.cs new file mode 100644 index 0000000..bc352c5 --- /dev/null +++ b/Forms1.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 + } + } +} diff --git a/TestWins/ConnectionSql.cs b/TestWins/ConnectionSql.cs new file mode 100644 index 0000000..dc4c3fa --- /dev/null +++ b/TestWins/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; + } +}