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
33 changes: 33 additions & 0 deletions TestWins/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Drawing.Text;
using System.Xml.Linq;
using TestWins.Controller;
using TestWins.Model;

namespace TestWins;

Expand All @@ -8,6 +10,7 @@ public partial class Form1 : Form
//business

private readonly StudentController controller = new StudentController();

public Form1()
{
InitializeComponent();
Expand All @@ -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();
}
}
}
22 changes: 21 additions & 1 deletion TestWins/model/ConnectionSql.cs
Original file line number Diff line number Diff line change
@@ -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()
{
Expand All @@ -16,7 +18,25 @@ public MySqlConnection connectSql()
_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");
}
}
}
}