diff --git a/ConnectionSql.cs b/ConnectionSql.cs new file mode 100644 index 0000000..ac9e18d --- /dev/null +++ b/ConnectionSql.cs @@ -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; + } +} \ No newline at end of file diff --git a/Form1.Designer.cs b/Form1.Designer.cs new file mode 100644 index 0000000..06a4723 --- /dev/null +++ b/Form1.Designer.cs @@ -0,0 +1,152 @@ +namespace TestWins; + +partial class Form1 +{ + // View + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + private System.Windows.Forms.TextBox txtStudentId; + private System.Windows.Forms.TextBox txtName; + private System.Windows.Forms.TextBox txtAge; + private System.Windows.Forms.TextBox txtCourse; + + + private System.Windows.Forms.Button btnAdd; + private System.Windows.Forms.Button btnUpdate; + private System.Windows.Forms.Button btnDelete; + + private System.Windows.Forms.DataGridView dataGridView1; + + private System.Windows.Forms.Label lblTitle; + + + + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + + this.txtName = new System.Windows.Forms.TextBox(); + this.txtAge = new System.Windows.Forms.TextBox(); + this.txtCourse = new System.Windows.Forms.TextBox(); + + this.btnAdd = new System.Windows.Forms.Button(); + this.btnUpdate = new System.Windows.Forms.Button(); + this.btnDelete = new System.Windows.Forms.Button(); + + this.dataGridView1 = new System.Windows.Forms.DataGridView(); + this.lblTitle = new System.Windows.Forms.Label(); + + //================ + // FORM + //=============== + + this.BackColor = System.Drawing.Color.WhiteSmoke; + this.ClientSize = new System.Drawing.Size(900, 600); + this.Text = "Student Management System"; + + //================ + // TITLE + //=============== + + this.lblTitle.Text = "Student Management System"; + //this.lblTitle.Font = new System.Drawing.Font("Segoe UI", 16F, System.Drawing.FOndStyle.Bold); + this.lblTitle.Location = new System.Drawing.Point(200, 10); + this.lblTitle.AutoSize = true; + + // =============== + // TEXTBOX + // ============== + + this.txtName.Location = new System.Drawing.Point(20, 110); + this.txtName.Size = new System.Drawing.Size(200, 23); + this.txtName.PlaceholderText = "Name"; + + this.txtAge.Location = new System.Drawing.Point(20, 150); + this.txtAge.Size = new System.Drawing.Size(200, 23); + this.txtAge.PlaceholderText = "Age"; + + this.txtCourse.Location = new System.Drawing.Point(20, 190); + this.txtCourse.Size = new System.Drawing.Size(200, 23); + this.txtCourse.PlaceholderText = "Course"; + + + // =============== + // BUTTONS + // ============== + + this.btnAdd.Text = "Add"; + this.btnAdd.Location = new System.Drawing.Point(20, 240); + this.btnAdd.Size = new System.Drawing.Size(60, 70); + this.btnAdd.BackColor = System.Drawing.Color.SeaGreen; + this.btnAdd.ForeColor = System.Drawing.Color.White; + this.btnAdd.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click); + + this.btnUpdate.Text = "Update"; + this.btnUpdate.Location = new System.Drawing.Point(90, 240); + this.btnUpdate.Size = new System.Drawing.Size(60, 30); + this.btnUpdate.BackColor = System.Drawing.Color.RoyalBlue; + this.btnUpdate.ForeColor = System.Drawing.Color.White; + this.btnUpdate.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click); + + this.btnDelete.Text = "Delete"; + this.btnDelete.Location = new System.Drawing.Point(160, 240); + this.btnDelete.Size = new System.Drawing.Size(60, 30); + this.btnDelete.BackColor = System.Drawing.Color.Firebrick; + this.btnDelete.ForeColor = System.Drawing.Color.White; + this.btnDelete.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click); + + // ======================= + // DATAGRIDVIEW + // ======================= + this.dataGridView1.Location = new System.Drawing.Point(250, 70); + this.dataGridView1.Size = new System.Drawing.Size(520, 350); + this.dataGridView1.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill; + this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullColumnSelect; + this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick); + + + //=================== + // ADD CONTROLS + //================== + + this.Controls.Add(this.lblTitle); + + this.Controls.Add(this.txtStudentId); + this.Controls.Add(this.txtName); + this.Controls.Add(this.txtAge); + this.Controls.Add(this.txtCourse); + + this.Controls.Add(this.btnAdd); + this.Controls.Add(this.btnUpdate); + this.Controls.Add(this.btnDelete); + + this.Controls.Add(this.dataGridView1); + } + + #endregion +} diff --git a/Form1.cs b/Form1.cs new file mode 100644 index 0000000..fe6f46f --- /dev/null +++ b/Form1.cs @@ -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 = ""; +} +} diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..83b3524 --- /dev/null +++ b/Program.cs @@ -0,0 +1,16 @@ +namespace TestWins; + +static class Program +{ + /// + /// The main entry point for the application. + /// + [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()); + } +} \ No newline at end of file diff --git a/TestWins.csproj b/TestWins.csproj new file mode 100644 index 0000000..dde5df0 --- /dev/null +++ b/TestWins.csproj @@ -0,0 +1,15 @@ + + + + WinExe + net10.0-windows + enable + true + enable + + + + + + + \ No newline at end of file diff --git a/TestWins.csproj.user b/TestWins.csproj.user new file mode 100644 index 0000000..f61322e --- /dev/null +++ b/TestWins.csproj.user @@ -0,0 +1,8 @@ + + + + + Form + + + diff --git a/winforms-sql-connection.sln b/winforms-sql-connection.sln new file mode 100644 index 0000000..96d910d --- /dev/null +++ b/winforms-sql-connection.sln @@ -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