using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using MySql.Data.MySqlClient; using MySql.Data; using System.Data; using System.Windows.Forms; using System.Security.Cryptography; using System.Configuration; using System.Threading; namespace MySQL_Database { internal class MySqlConnector { public static MySqlConnection GetConnection() { string connString = "datasource=localhost;port=3306;username=root;password=;database=students;"; MySqlConnection conn = new MySqlConnection(connString); try { conn.Open(); }catch(MySqlException ex) { MessageBox.Show("MySQL Connection! \n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return conn; } public static void AddStudent(string stdid, string fullname, int age, string sex, string address, string email, int contact, string stdtype, string yearLevel, string program, int creds, string password) { string sql = "INSERT INTO studentinfo (student_id, full_name, age, sex, address, email, contact_no, student_type, year_level, program, role_credentials, studentpass) VALUE (@stdid, @fullname, @age, @sex, @address, @email, @contact, @stdtype, @yearLevel, @program, @creds, @password)"; using (MySqlConnection conn = GetConnection()) { try { using (MySqlCommand cmd = new MySqlCommand(sql ,conn)) { cmd.Parameters.AddWithValue("@student_id", stdid); cmd.Parameters.AddWithValue("@full_name", fullname); cmd.Parameters.AddWithValue("@age", age); cmd.Parameters.AddWithValue("@sex", sex); cmd.Parameters.AddWithValue("@address", address); cmd.Parameters.AddWithValue("@email", email); cmd.Parameters.AddWithValue("@contact_no", contact); cmd.Parameters.AddWithValue("@student_type", stdtype); cmd.Parameters.AddWithValue("@year_level", yearLevel); cmd.Parameters.AddWithValue("@program", program); cmd.Parameters.AddWithValue("@roles_credentials", creds); cmd.Parameters.AddWithValue("@studentpass", password); int rosAffected = cmd.ExecuteNonQuery(); } } catch (Exception ex) { MessageBox.Show("Student not added!\n"+ex.Message, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } conn.Close(); } } public void LoginAccount(string email, string password) { string firstChar = email.ToString().Trim()[0].ToString(); if (int.TryParse(firstChar, out int result)) { if(CheckData(email, password)) { IndividualStudentInfo indi = new IndividualStudentInfo(); indi.Show(); } MessageBox.Show("Authentication successful!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); }else if (CheckData(email, password)) { IndividualStudentInfo indi = new IndividualStudentInfo(); indi.Show(); //studinfoForm admin = new studinfoForm(); //admin.Show(); } else { MessageBox.Show("Invalid email or password!", "Stop", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private bool CheckData(string email, string pass) { bool exist = false; using (MySqlConnection conn = GetConnection()) { try { //string qry = "SELECT * FROM credentials WHERE studentID = @email AND password;"; //using (MySqlCommand cmd = new MySqlCommand(qry, conn)) //{ // cmd.Parameters.AddWithValue("@studentID", email); // cmd.Parameters.AddWithValue("@password", pass); // int count = Convert.ToInt32(cmd.ExecuteScalar()); // exist = count > 0; //} string qry = "SELECT * FROM credentials WHERE admin = @email OR studentID = @email AND password = @password"; using (MySqlCommand cmd = new MySqlCommand(qry, conn)) { cmd.Parameters.AddWithValue("@email", email); cmd.Parameters.AddWithValue("@studentID", email); cmd.Parameters.AddWithValue("@password", pass); int count = Convert.ToInt32(cmd.ExecuteScalar()); exist = count > 0; MySqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { return true; } else { return false; } } }catch(Exception ex) { MessageBox.Show("Error: " + ex.Message); } conn.Close(); } return exist; } } }