the one Posted May 21, 2009 at 01:32 PM Report #266132 Posted May 21, 2009 at 01:32 PM 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 .
jpaulino Posted May 21, 2009 at 01:37 PM Report #266136 Posted May 21, 2009 at 01:37 PM Usas um ExecuteScalar() e fazes algo do género: SELECT COUNT(*) FROM tabela WHERE campo = '" & Me.TextBox1.Text & "';" Se o resultado for < 1 então o campo não existe!
the one Posted May 21, 2009 at 02:04 PM Author Report #266148 Posted May 21, 2009 at 02:04 PM 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now