Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions TestWins/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System.Drawing.Text;
using TestWins.Controller;

namespace TestWins;

public partial class Form1 : Form
{
//business

private readonly StudentController controller = new StudentController();

public Form1()
{
InitializeComponent();
Expand All @@ -21,23 +19,46 @@ private void loadData()

private void btnAdd_Click(object sender, EventArgs e)
{

controller.add(txtName.Text, txtCourse.Text);
loadData();
clearFields();
}

private void btnUpdate_Click(object sender, EventArgs e)
{

int id = int.Parse(txtId.Text);
controller.update(id, txtName.Text, txtCourse.Text);
loadData();
clearFields();
}


private void btnDelete_Click(object sender, EventArgs e)
{
int id = int.Parse(txtId.Text);
controller.delete(id);
loadData();
clearFields();
}


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();
}
}

private void dataGridView1_CellClick(object sender, EventArgs e)

private void clearFields()
{

txtId.Clear();
txtName.Clear();
txtCourse.Clear();
}
}
34 changes: 20 additions & 14 deletions TestWins/model/ConnectionSql.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
namespace TestWins.Model;

//dotnet add package MySql.Data
using MySql.Data.MySqlClient;

public class ConnectionSql
namespace TestWins.Model
{
private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root";
private MySqlConnection _conn;

public MySqlConnection connectSql()
public class ConnectionSql
{
private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root";
private MySqlConnection _conn;

Console.WriteLine("Connecting to DB");

_conn = new MySqlConnection(_connectionString);
public MySqlConnection ConnectSql()
{
try
{
Console.WriteLine("Connecting to DB...");

Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful");
_conn = new MySqlConnection(_connectionString);
_conn.Open(); //
Console.WriteLine("Database connection successful");
}
catch (Exception ex)
{
Console.WriteLine("Database connection failed: " + ex.Message);
}

return _conn;
return _conn;
}
}
}
}