Как предотвратить вставку дубликатов записей в БД
проблема с моим кодом заключается в том, что когда я нажимаю кнопку сохранить его, я вставляю дубликаты записей
я не знаю, как я могу исправить это
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; using System.IO; namespace Carprogram { public partial class Form1 : Form { SqlConnection mm = new SqlConnection("Data Source=NAWAF;Initial Catalog=CAR;Integrated Security=True"); DataTable dt = new DataTable(); public Form1() { InitializeComponent(); } string imgloc=""; SqlCommand cmd; private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { OpenFileDialog dialog = new OpenFileDialog (); dialog.Filter = "png files(*.png)|*.png|jpg files(*.jpg)|*.jpg|All files(*.*)|*.*"; if(dialog.ShowDialog()==DialogResult.OK) { imgloc =dialog.FileName.ToString(); pictureBox1.ImageLocation=imgloc; } } private void groupBox1_Enter(object sender, EventArgs e) { } private void save_Click(object sender, EventArgs e) { string stat = "active"; byte[] images = null; FileStream Streem = new FileStream(imgloc, FileMode.Open, FileAccess.Read); BinaryReader brs = new BinaryReader(Streem); images = brs.ReadBytes((int)Streem.Length); mm.Open(); string vmd = "Insert into vehicleinfo(modely,make,model,color,type,odometer,odoty,vin,vehicle,driverop,department,engine,transmission,tiresize,platlic,renewal,company,account,premium,due,note,img,status)VALUES('" + modely.Text + "' ,'" + make.Text + "' , '" + model.Text + "' , '" + color.Text + "' , '" + type.Text + "','" + odometer.Text + "' ,'" + odoty.Text + "','" + vin.Text + "' , '" + vehicle.Text + "' , '" + driverop.Text + "' , '" + department.Text + "','" + engine.Text + "','" + transmission.Text + "','" + tiresize.Text + "','" + platlic.Text + "','" + renewal.Text + "','" + company.Text + "','" + account.Text + "','" + premium.Text + "','" + due.Text + "','" + note.Text + "',@images,@stat)"; cmd = new SqlCommand (vmd,mm); cmd.Parameters.Add(new SqlParameter("@images",images)); cmd.Parameters.Add(new SqlParameter("@stat", stat)); cmd.ExecuteNonQuery(); int N = cmd.ExecuteNonQuery(); mm.Close(); MessageBox.Show(N.ToString() + "Data Saved"); modely.Text =""; make.Text =""; model.Text =""; color.Text =""; type.Text =""; odometer.Text=""; vin.Text =""; vehicle.Text =""; driverop.Text =""; department.Text =""; engine.Text =""; transmission.Text=""; tiresize.Text =""; platlic.Text =""; renewal.Text =""; company.Text =""; premium.Text =""; due.Text = ""; note.Text = ""; } private void Form1_Load(object sender, EventArgs e) { fillData(); } private void fillData() { SqlDataAdapter adapter = new SqlDataAdapter(); SqlCommand rrm; string sql = "select * from vehicleinfo"; rrm = new SqlCommand(sql, mm); adapter.SelectCommand = rrm; adapter.Fill(dt); dataGridView1.DataSource = dt; } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { DataView dv = new DataView(dt); if(comboBox1.SelectedItem.ToString()=="Show ALL") { dataGridView1.DataSource = dt; } else { dv.RowFilter = string.Format("status LIKE '%0%'", comboBox1.SelectedItem.ToString()); } } } }
Что я уже пробовал:
я хочу одну запись, а не дубликат