简体   繁体   English

C#对象唯一

[英]C# object unique

In my asp.net web applicaiton I am using objects to add infomation to database something like this 在我的asp.net Web应用程序中,我正在使用对象将信息添加到数据库中,如下所示

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. 然后,用户打电话说:“我输入的年龄与为员工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. 在这种情况下,您可以为ID使用整数值,这是使用SQL Server的某些内置功能以相对干净的方式对所有员工进行排序的一种方法。

  2. GUIDs. GUID。 In this case, you'd assign a new GUID to each employee and have this be a unique value for each employee. 在这种情况下,您将为每个员工分配一个新的GUID,并将其作为每个员工的唯一值。 This can be done in the DB though you could also do it in the C# code if you wanted. 这可以在数据库中完成,尽管您也可以在C#代码中完成。

You can make the object Unique by using Guid type. 您可以使用Guid类型使对象唯一。 emp1.Guid = Guid.NewGuid(); 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 您可以在此处阅读有关SQL密钥的信息

update yourTable
SET
  YourColumnName = YourValue
Where
  tableId = Idofthisemployee

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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