Jump to content

Recommended Posts

Posted

Boa tarde pessoal .

Como é que eu faco uma verificação numa tabela da base de dados , se aquele campo já é existente ?

Passo a explicar-me, tenho numa textbox um nome, e tenho de verificar se esse nome já existe em algum campo de uma tabela .

Posted
SqlConnection conn1 = new SqlConnection(connectionString);
            conn1.Open();
            SqlCommand cmd1 = new SqlCommand("SELECT COUNT (*) FROM utilizadores WHERE username = @username", conn1);

            cmd1.Parameters.Add("@username", SqlDbType.NVarChar).Value = txtuser.Text;

            int i = (int)cmd1.ExecuteScalar();

            // Se i for maior que zero é porque achou o usuario/senha, senao não acho
            if (i > 0)
            {
                MessageBox.Show("Utilizador já existente");
            }
            else
            {
                MessageBox.Show("O utilizaodr não existe");
            }

Topic Solved .

Obrigado

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
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.