Note that there are some explanatory texts on larger screens.

plurals
  1. POcheck for duplicate records in a sql server table using c#
    primarykey
    data
    text
    <p>I have a table in SQL server that contains string data on two columns. I also have a "add" method in c# , but I don't what to be able to add the same data on the two columns again.For example in the table I have data similar to this: </p> <pre class="lang-none prettyprint-override"><code>code first_name last_name 1 john smith 2 mike croft </code></pre> <p>I don't want to have the possibility to add john smith or mike croft again even if the code is different. I'm kind of new to c# so if someone could give me a simple answer in a code I would really appreciate it.</p> <p>P.S. The code is the primary key , and I have other columns as well. </p> <p>This is the code i have :</p> <pre><code> Form1 obj = (Form1)Application.OpenForms["Form1"]; private void button2_Click(object sender, EventArgs e) { SqlConnection cn = new SqlConnection(@" Data Source=HOME-D2CADC8D4F\SQL;Initial Catalog=motociclete;Integrated Security=True"); MemoryStream ms = new MemoryStream(); pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); byte[] pic_arr = new byte[ms.Length]; ms.Position = 0; ms.Read(pic_arr, 0, pic_arr.Length); SqlCommand cmd = new SqlCommand("insert into motociclete(firma,model,poza,pret,anf,greutate,caprez,putere,garantie,stoc) values (@firma,@model,@poza,@pret,@anf,@greutate,@caprez,@putere,@garantie,@stoc)",cn); cmd.Parameters.AddWithValue("@firma", textBox3.Text); cmd.Parameters.AddWithValue("@model", textBox10.Text); cmd.Parameters.AddWithValue("@poza", pic_arr); cmd.Parameters.AddWithValue("@pret", textBox7.Text); cmd.Parameters.AddWithValue("@anf", textBox4.Text); cmd.Parameters.AddWithValue("@greutate", textBox9.Text); cmd.Parameters.AddWithValue("@caprez", textBox5.Text); cmd.Parameters.AddWithValue("@putere", textBox8.Text); cmd.Parameters.AddWithValue("@garantie", textBox6.Text); cmd.Parameters.AddWithValue("@stoc", textBox2.Text); cn.Open(); try { int rez = cmd.ExecuteNonQuery(); if (rez &gt; 0) { MessageBox.Show("Adaugare reusita "); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { cn.Close(); obj.loaddata(); this.Close(); } } } </code></pre> <p>I don't want to be able to add two records with the same "firma" and "model".But it's ok if one of them repeats itself. I hope the code helps.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload