User registration in WinForms through the database.

I can’t figure it out, I made registration in WinForms through the SQL server management studio database, I need to make it so that you cannot register the data that has already been registered and are in the database, help!

private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" || textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "") { MessageBox.Show("Пожалуйста, заполните все поля для регистрации!!!"); } else { SqlConnection con = new SqlConnection(@"Data Source=sql2016;Initial Catalog=Stolovka;Integrated Security=True;"); con.Open(); string str = "insert into Users(Login,Password,Name,Surname,Patronymic,Telefon) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')"; SqlCommand cmd = new SqlCommand(str, con); cmd.ExecuteNonQuery(); MessageBox.Show("Пользователь зарегистрирован!!!"); } } 
  • one
    Make Unique constraint on the required fields. When inserting, catch a possible exception. - Alexander Petrov

1 answer 1

Everything is simple: you should make a query (select) in the database with the condition for those fields that should not be repeated. If the request returns something, therefore such data has already been entered))

I sincerely hope that this code is only a training code and is not used in combat, otherwise kites will fly in and they will peck at you))