Comparison with Postgresql database using C #

7

I created a Login application, I made the connection to the database, I get the user and password registered in my bank. But what I want is, if the user tries more than three times to get in and has something wrong, block the textbox and button .

You would have to make the comparison with the user registered with the bank.

Follow the code as far as I've done.

    {
        bool blnFound = false;
        NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=****;Database=HHH");
        conn.Open(); //abrir conexão
        NpgsqlCommand cmd = new NpgsqlCommand("select * from login where nome = '" + txtUserName.Text + "' and senha = '" + txtSenha.Text + "'",conn);
        NpgsqlDataReader dr = cmd.ExecuteReader();

        if(dr.Read())
        {
            blnFound = true;
            Principal pc = new Principal();
            pc.Show();
            this.Hide();
        }
        if(blnFound==false)
            MessageBox.Show("Nome ou senha estão incorrentos!","Erro",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
        dr.Close();
        conn.Close();
    }

When the user, erases his password or his user, the textbox would be disabled. More or less like this: txtUserName.Text.Enabled = false

    
asked by anonymous 09.07.2015 / 21:07

2 answers

5

You can add a variable in the code if you do not want to add a column in the database to check how many times the login was attempted.

int tentativas = 0;

{
    NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;Port=5432;User Id=postgres;Password=****;Database=HHH");
    conn.Open();
    NpgsqlCommand cmd = new NpgsqlCommand("select * from login where nome = '" + txtUserName.Text + "' and senha = '" + txtSenha.Text + "'",conn);
    NpgsqlDataReader dr = cmd.ExecuteReader();

    if(dr.Read())
    {
        Principal pc = new Principal();
        pc.Show();
        this.Hide();
    }
    else
    {
        MessageBox.Show("Nome ou senha estão incorretos!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        tentativas++;
    }

    if (tentativas > 2)
    {
        MessageBox.Show("Login bloqueado")
        button1.Enabled = False;
    }   

    dr.Close();
    conn.Close();
}
    
23.11.2015 / 23:09
1

Try to make a function for this routine. Create a loop to test the user and password in the amount of times you want (or set for the system), so you can do the validation, if the validation exceeds the set amount, can lock the system, display a system screen with error, or a message, and so on.

    
13.07.2015 / 22:44