简体   繁体   中英

C# object unique

In my asp.net web applicaiton I am using objects to add infomation to database something like this

Employee emp1 = new Employee()
emp1.name = "something"
emp1.age = some age
InsertEmployee(emp)

This code works fine. But however if multiple users are trying to insert the employee data then data gets mixed up.

User then calls up and says , "The age I entered is different from what it is showing for employee XXX.

How to fix this ? Whats the good method to make the object unique ?.

Thanks....

There are a couple of different ideas here:

  1. Primary key in the database. In this case, you could use an integer value for the ID and this is a way to keep all the employees sorted in a relatively clean manner using some of SQL Server's built-in functionality.

  2. GUIDs. In this case, you'd assign a new GUID to each employee and have this be a unique value for each employee. This can be done in the DB though you could also do it in the C# code if you wanted.

You can make the object Unique by using Guid type. emp1.Guid = Guid.NewGuid();

User then calls up and says , "The age I entered is different from what it is showing for employee XXX.

How are you updating the user results?

 How to fix this ? Whats the good method to make the object unique ?.

You need to add primary key in your database. And do all your database operations against that primary key. You can read about SQL Keys here .Example

update yourTable
SET
  YourColumnName = YourValue
Where
  tableId = Idofthisemployee

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM