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
48 changes: 39 additions & 9 deletions TestWins/Form1.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
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 +18,56 @@ private void loadData()

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 = "";
}
}
25 changes: 14 additions & 11 deletions TestWins/model/ConnectionSql.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ 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()
public class ConnectionSql
{
private readonly string _connectionString = "server=localhost;database=student;uid=root;pwd=root;";
private MySqlConnection _conn;

Console.WriteLine("Connecting to DB");
public MySqlConnection connectSql()
{
Console.WriteLine("Connecting to DB");

_conn = new MySqlConnection(_connectionString);
_conn = new MySqlConnection(_connectionString);
_conn.Open();

Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful");
Console.WriteLine(_conn.State == System.Data.ConnectionState.Open
? "Database connection successful"
: "Database Connection Failed");

return _conn;
return _conn;
}
}
}