违反PRIMARY KEY约束'PK_LogIn'。无法在对象'dbo.LogIn'中插入重复键。 [英] Violation of PRIMARY KEY constraint 'PK_LogIn'. Cannot insert duplicate key in object 'dbo.LogIn'.

查看:180
本文介绍了违反PRIMARY KEY约束'PK_LogIn'。无法在对象'dbo.LogIn'中插入重复键。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



i创建了登录页面和一个注册表单。在那个注册表单提交按钮就在那里。我写了这段代码请检查一次并解决我的问题。



Hi,
i created login page and one registration form also.In that registration form submit button is there.I wrote this code please check it once and solve my problem.

protected void Button1_Click(object sender, EventArgs e)
{
    con.Open();
    cmd = new SqlCommand("insert into LogIn Values('" + txtfname.Text + "','" + txtlname.Text + "','" + txtuname.Text + "','" + txtpwd.Text + "','" + txtcpwd.Text + "')", con);
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.AddWithValue("FirstName", txtfname.Text);
    cmd.Parameters.AddWithValue("LastName", txtlname.Text);
    cmd.Parameters.AddWithValue("UserName", txtuname.Text);
    cmd.Parameters.AddWithValue("PassWord", txtpwd.Text);
    cmd.Parameters.AddWithValue("ConfirmPassWord", txtcpwd.Text);
    cmd.ExecuteNonQuery();
    da = new SqlDataAdapter("Select * from LogIn", con);
    ds = new DataSet();
    da.Fill(ds, "LogIn");
    Response.Write("Registration Completed Successfully");
    clear();
    con.Close();
}





错误:

违反PRIMARY KEY约束'PK_LogIn'。无法在对象'dbo.LogIn'中插入重复键。

语句已被终止



Error:
Violation of PRIMARY KEY constraint 'PK_LogIn'. Cannot insert duplicate key in object 'dbo.LogIn'.
The statement has been terminated

推荐答案

似乎每个人都希望您查询数据库在插入之前,请这样做!假设用户名是你的主键,你需要捕获异常,然后告诉用户用户名已被占用。



你不想要的原因检查然后插入是因为你将拥有所谓的竞争条件。

你可能有2个用户在大约时间创建一个帐户,因此你的代码将在两个不同的线程上运行两次。因此,您可以遇到这样的情况:2个线程执行代码,该代码检查在任一线程插入值之前是否采用了用户名,因此一个线程抛出异常。
Seems everyone wants you to query the database prior to inserting, do not do that! Assuming username is your primary key, you'll want to catch the exception and then tell the user that the username is already taken.

The reason you don't want to check and then insert is because you'll have what is called a Race Condition.
You might have 2 users creating an account at approximately the smae time, thus your code will run twice on two different threads. Thus you can have a situation where the 2 threads execute the code that checks if the username is taken before either thread inserts a value, so one thread will throw the exception.


您尝试插入的用户名已经存在,因为它是主键,所以它不允许重复。



插入数据库之前,检查用户标识是否已存在。如果没有,则插入数据。
the user name which you are trying to insert already exists and since it is the primary key, it does not allow duplicates.

before inserting in the database, check the userid already exists or not. If not then insert data.


显然问题出在您试图保留的数据中。如果可能,请更改声明为primary的字段的值,或者使用您尝试插入的键删除记录。否则使用更新!





干杯
Clearly the problem is in the data you are trying to persist. If possible change the value of the field that is declared as primary or remove the record with the key you are trying to insert. Otherwise use an update!


Cheers


这篇关于违反PRIMARY KEY约束'PK_LogIn'。无法在对象'dbo.LogIn'中插入重复键。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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