Comparison with Postgresql database using C#

Asked

Viewed 249 times

7

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

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

Follow the code to where I made it.

    {
        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 misses their password or their user, the textbox would be disabled. Sort of like this: txtUserName.Text.Enabled = false

  • 2

    Of course, all you have to do is edit the question, post everything you’ve ever done and tell us where you’re tangled up. Maybe there’s a way even better than what you’re doing but we need more details.

2 answers

5


You can add a variable in the code if you don’t want to add a column in the database to check how many times the login has been tried.

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();
}

1

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.