如何使用C#windows应用程序防止插入重复记录SQL Server [英] How to prevent insert of duplicated records SQL server using C# windows application

查看:97
本文介绍了如何使用C#windows应用程序防止插入重复记录SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在textbox txtKVNumber中插入相同数据时出错。错误如下:发生错误,请稍后再试。违反PRIMARY KEY约束'PK_khaas'。无法在对象'dbo.khaas'中插入重复键。该声明已被终止。



我尝试过:



I'm getting an error when I insert same data in textbox txtKVNumber. The error is like: An error has occurred, please try again later. Violation of PRIMARY KEY constraint 'PK_khaas'. Cannot insert duplicate key in object 'dbo.khaas'. The statement has been terminated.

What I have tried:

private void btnKSave_Click(object sender, EventArgs e)
       {
           try
           {
               if (txtKVNumber.Text == "")
               {
                   string myStringVariable1 = string.Empty;
                   MessageBox.Show("Vehicle Number is required");
               }
               else if (cboKVColor.Text == "")
               {
                   string myStringVariable2 = string.Empty;
                   MessageBox.Show("Select Vehicle Color");
               }
               else if (cboKVBrand.Text == "")
               {
                   string myStringVariable3 = string.Empty;
                   MessageBox.Show("Select Vehicle Brand");
               }
               else if (cboKVType.Text == "")
               {
                   string myStringVariable12 = string.Empty;
                   MessageBox.Show("Select Vehicle Type");
               }
               else if (txtKOName.Text == "")
               {
                   string myStringVariable5 = string.Empty;
                   MessageBox.Show("Owner Name is required");
               }
               else if (txtKCivilID.Text == "")
               {
                   string myStringVariable7 = string.Empty;
                   MessageBox.Show("Civil ID is required");
               }
               else if (txtKTelephone.Text == "")
               {
                   string myStringVariable8 = string.Empty;
                   MessageBox.Show("Telephone Number is required");
               }

               else
               {
                   command = new SqlCommand("insert into khaas( VNumber, VColor, VType, VBrand, ExpiryDate, DaysLeft, OName, CivilID, Telephone ) VALUES (@vnumber, @vcolor, @vtype, @vbrand, @expirydate, @daysleft, @ownername, @civilid, @telephone )", con);
                   con.Open();
                   command.Parameters.AddWithValue("@vnumber", txtKVNumber.Text);
                   command.Parameters.AddWithValue("@vcolor", cboKVColor.Text);
                   command.Parameters.AddWithValue("@vtype", cboKVType.Text);
                   command.Parameters.AddWithValue("@vbrand", cboKVBrand.Text);
                   command.Parameters.AddWithValue("@expirydate", dateTimePickerKhaas.Value.ToString("MM/dd/yyyy"));
                   command.Parameters.AddWithValue("@daysleft", txtKDaysLeft.Text);
                   command.Parameters.AddWithValue("@ownername", txtKOName.Text);
                   command.Parameters.AddWithValue("@civilid", txtKCivilID.Text);
                   command.Parameters.AddWithValue("@telephone", txtKTelephone.Text);
                   command.ExecuteNonQuery();
                   con.Close();
                   MessageBox.Show("Inserted Successfully");
                   grd_fillKhaas();
                   txtKVNumber.Text = "";
                   cboKVColor.Text = "";
                   cboKVType.Text = "";
                   cboKVBrand.Text = "";
                   dateTimePickerKhaas.Value = DateTime.Now;
                   txtKDaysLeft.Text = "";
                   txtKOName.Text = "";
                   txtKCivilID.Text = "";
                   txtKTelephone.Text = "";
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show("An error has occurred, please try again later." + ex.Message);

           }
           finally
           {
               con.Close();
           }
       }

推荐答案

Quote:

违反PRIMARY KEY约束'PK_khaas'。无法在对象'dbo.khaas'中插入重复键。该声明已被终止。

Violation of PRIMARY KEY constraint 'PK_khaas'. Cannot insert duplicate key in object 'dbo.khaas'. The statement has been terminated.



这是 PRIMARY KEY 的原则。

原则上,SQL不允许2条记录共享相同的PRIMARY KEY,因为它是识别每个记录的值,因此必须是唯一的。


That is the principle of PRIMARY KEY.
By principle, SQL do not allow 2 records to share the same PRIMARY KEY as it is the value identifying each records and thus must be unique.

Quote:

如何防止使用C#windows应用程序插入重复记录SQL服务器

How to prevent insert of duplicated records SQL server using C# windows application



您可以在插入新记录之前检查密钥是否存在。

甚至更好,有server为您生成一个唯一的密钥。


You can check if key exist before inserting new record.
Or even better, have the server generate a unique key for you.


这篇关于如何使用C#windows应用程序防止插入重复记录SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆