我需要编写一个简单的数据网格表单,允许用户编辑单元格 - 问题是不同的人可以同时编辑它,因此并发性成为一个问题
我正在使用数据库支持的数据
我似乎有两个选择:
1) 在允许编辑之前不断轮询数据库并刷新数据网格中的数据 - 这意味着需要在编辑后立即更新数据库,而不是允许编辑、然后让用户查看然后提交的首选方式。
2) 允许脏编辑,然后阻止用户向数据库提交他们想要的更改
任何人都可以描述一种允许用户同时编辑行的机制,这将使实现变得容易吗?
编辑:我的问题是如何在 C# 中实现这一点 - 我已经添加了锁定列,但这仍然不够 - 如果 usera 尝试编辑 row1 并提交更改 userB 尝试编辑 row1 的过时版本,这将不会被捕获这是一个大问题
最佳答案
您有以下选项,但不限于:
Optimistic Concurrency - assume that while there may be concurrency conflicts every now and then, the vast majority of the time such conflicts won't arise; therefore, if a conflict does arise, simply inform the user that their changes can't be saved because another user has modified the same data
Pessimistic Concurrency - assume that concurrency conflicts are commonplace and that users won't tolerate being told their changes weren't saved due to another user's concurrent activity; therefore, when one user starts updating a record, lock it, thereby preventing any other users from editing or deleting that record until the user commits their modifications
有关引用和详细信息,请参阅:
乐观并发
PeopleDataContext people = new PeopleDataContext();
Person p = people.People.Single(person => person.ID == 1);
p.IDRole = 2;
try
{ people.SubmitChanges(ConflictMode.ContinueOnConflict); }
catch (ChangeConflictException cce)
{ people.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges); }
悲观并发
PeopleDataContext people = new PeopleDataContext();
using (TransactionScope t = new TransactionScope())
{
Person p = people.People.Single(person => person.ID == 1);
p.LastName = "Pessimistic";
p.FirstName = "Concurrency";
people.SubmitChanges();
t.Complete();
}
引用:
关于c# - DataGrid - 防止并发访问行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2262917/