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
64 changes: 64 additions & 0 deletions Forms1.cs
Original file line number Diff line number Diff line change
@@ -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
}
}
}
44 changes: 44 additions & 0 deletions TestWins/ConnectionSql.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}