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
22 changes: 22 additions & 0 deletions ConnectionSql.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace TestWins.Model;

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

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

public MySqlConnection connectSql()
{

Console.WriteLine("Connecting to DB");

_conn = new MySqlConnection(_connectionString);

Console.WriteLine(_conn == null ? "Datbase Connection Failed" : "Database connection successful");

return _conn;
}
}
152 changes: 152 additions & 0 deletions Form1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

111 changes: 111 additions & 0 deletions Form1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System.Drawing.Text;
using TestWins.Controller;

namespace TestWins;

public partial class Form1 : Form
{
//business

private readonly StudentController controller = new StudentController();
public Form1()
{
InitializeComponent();


dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
dataGridView1.AutoGenerateColumns = true;
dataGridView1.MultiSelect = false;

loadData();
}

private void loadData()
{
try
{
dataGridView1.DataSource = controller.getAll();
}
catch (Exception ex)
{
MessageBox.Show("Error loading data: " + ex.Message);
}
}

private void btnAdd_Click(object sender, EventArgs e)
{
try
{
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();
}
catch (Exception ex)
{
MessageBox.Show("Error adding student: " + ex.Message);
}
}

private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
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();
}
catch (Exception ex)
{
MessageBox.Show("Error updating student: " + ex.Message);
}
}

private void btnDelete_Click(object sender, EventArgs e)
{
try
{
controller.delete(txtStudentId.Text);
loadData();
clearFields();
}
catch (Exception ex)
{
MessageBox.Show("Error deleting student: " + ex.Message);
}
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs 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 = "";
}
}
16 changes: 16 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace TestWins;

static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
15 changes: 15 additions & 0 deletions TestWins.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net10.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MySql.Data" Version="9.6.0" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions TestWins.csproj.user
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Update="Form1.cs">
<SubType>Form</SubType>
</Compile>
</ItemGroup>
</Project>
24 changes: 24 additions & 0 deletions winforms-sql-connection.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.2.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestWins", "TestWins\TestWins.csproj", "{27D407B4-FF31-33C0-D274-32E053605027}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{27D407B4-FF31-33C0-D274-32E053605027}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{27D407B4-FF31-33C0-D274-32E053605027}.Debug|Any CPU.Build.0 = Debug|Any CPU
{27D407B4-FF31-33C0-D274-32E053605027}.Release|Any CPU.ActiveCfg = Release|Any CPU
{27D407B4-FF31-33C0-D274-32E053605027}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0FB4CEA9-6018-405A-B2FE-C4C60BAC6F71}
EndGlobalSection
EndGlobal