开发者

Avoid inserting duplicate records in c# [duplicate]

This question already exists: Closed 11 years ago.

How to avoid inserting duplicate records in c#

For exampl开发者_JAVA技巧e:

Table name: record1 in that userName is primary key

  1. insert record1.(userName=101, password=raj, confirmPassword=raj)
  2. insert record2.(userName=102, password=kumar, confirmPassword=kumar)
  3. insert record3.(userName=101, password=jose, confirmPassword=jose)

When the ok button is clicked it shows a message like

MessageBox.Show("User name already inserted. Please change username");


Create an index on the table on the userName column and make it Unique. This will throw an exception when a duplicate record is added.


Are you familiar with using a database system? Maybe I'm misunderstanding you, but all you would do is run a query to see if the user exists or check on the SQL error returned if you have a constraint on the username.

If that is indeed what you were looking for, I think you should learn to use Google more often and learn SQL before diving into writing a system that requires it. :)


You could catch the primary key violation and handle it with your error message.


  • Validate in your usertable (via a query) that the username does not exist yet
  • If not, insert, else show the messagebox
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜