antiblock
diamwall
  • Chatbox

    You don't have permission to chat.
    Load More
Sign in to follow this  
Snooke

[Database] Registar

1 post in this topic

Boas Pessoal.

Estou a aprender MySQL usando o MySQLWorkbench e c#.

Eu estou a fazer um sistema de login e registo muito simples, mas quando meto registar dá sempre "registo falhado", but se eu adicionar os dados manualmente na tabela e fizer login já funciona :|

 

Código do Registar:


string query = $"INSERT INTO login (id, username, password) VALUES ('','{user}','{pass}');";
 
            try
            {
                if (OpenConnection())
                {
                    MySqlCommand cmd = new MySqlCommand(query, conn);
 
                    try
                    {
                        cmd.ExecuteNonQuery();
                        return true;
                    }
                    catch(Exception ex)
                    {
                        return false;
                    }
                }
                else
                {
                    conn.Close();
                    return false;
                }
            }
            catch
            {
                conn.Close();
                return false;
            }

 

Código do Login:

string query = $"SELECT * FROM login WHERE username= '{user}' AND password ='{pass}';";
 
            try
            {
                if (OpenConnection())
                {
                    MySqlCommand cmd = new MySqlCommand(query, conn);
                    MySqlDataReader reader = cmd.ExecuteReader();
 
                    if (reader.Read())
                    {
                        reader.Close();
                        conn.Close();
                        return true;
                    }
                    else
                    {
                        reader.Close();
                        conn.Close();
                        return false;
                    }
                }
                else
                {
                    conn.Close();
                    return false;
                }
            }
            catch(Exception ex)
            {
                conn.Close();
                return false;
            }

 

Código que coloquei no botão Registar

string user = textBox1.Text;
            string pass = textBox2.Text;
 
            if (Register(user, pass))
            {
                MessageBox.Show($"User {user} has been created.");
            }
            else
            {
                MessageBox.Show($"User {user} has not been created.");
            }

 

Código do botão Logar

string user = textBox1.Text;
            string pass = textBox2.Text;
 
            if (IsLogin(user, pass))
            {
                MessageBox.Show($"Welcome {user} !");
            }
            else
            {
                MessageBox.Show($"User {user} does not exist or password is incorrect.");
            }

 

Podiam-me dar uma ajuda?

Obrigado.

 

Abraço,

André

Share this post


Link to post
Share on other sites
antiblock
Rodnia | Alpha & Omega

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this