Exemple #1
0
        public bool cekLogin(string username, string password)
        {
            Database        db    = new Database();
            MySqlConnection conn  = db.connect(db.getString());
            bool            benar = false;

            try
            {
                conn.Open();
                string       query = "SELECT * FROM user WHERE username = @username AND password = @password";
                MySqlCommand cmd   = new MySqlCommand(query, conn);
                cmd.Parameters.AddWithValue("@username", username);
                cmd.Parameters.AddWithValue("@password", password);

                MySqlDataReader reader = cmd.ExecuteReader();
                if (!reader.Read())
                {
                    MessageBox.Show("Username atau Password salah. Silahkan ulangi kembali.", "Kesalahan");
                }
                else
                {
                    benar     = true;
                    id_user   = reader[0].ToString();
                    hak_akses = reader[4].ToString();
                    if (hak_akses == "Admin")
                    {
                        FormLogin     form_login = new FormLogin();
                        FormMainAdmin main       = new FormMainAdmin();
                        main.Show();
                    }
                    else if (hak_akses == "User")
                    {
                        FormLogin form_login = new FormLogin();
                        FormMain  main       = new FormMain();
                        main.Show();
                    }
                }
                conn.Close();
            }
            catch (MySqlException)
            {
                db.createDatabase();
                this.cekLogin(username, password);
            }
            return(benar);
        }