简体   繁体   English

如何在多用户环境中更新/删除行

[英]How to update/delete row in multi user environment

I am new to C# and SQL Server . 我是C#SQL Server新手。

I am developing an application using Winforms . 我正在使用Winforms开发应用程序。

I am using dataset . 我正在使用dataset

In master and details transactions, suppose I retrieve one transaction from the database. 在主交易和明细交易中,假设我从数据库中检索了一个交易。 At the same time another user also retrieves the same transaction. 同时,另一个用户也检索相同的交易。

My requirement is when I am changing this transaction no one else should be allowed to update or delete the same transaction. 我的要求是,当我更改此交易时,不允许任何人更新或删除同一交易。

As dataset is in disconnect mode, how can I achieve the locking mechanism? 由于dataset处于disconnect模式,如何实现锁定机制?

If im correct in assuming you are using C# then you should look into some ORM frameworks as they can handle collisions like this for you or at least alert you when they have happened so you can handle them in your code. 如果不能正确地假设您正在使用C#,那么您应该研究一些ORM框架,因为它们可以为您处理此类冲突,或者至少在发生冲突时提醒您,以便您可以在代码中进行处理。 So you could for instance inform the user someone else has made a change and refresh their display or merge the changes and save the merged data. 因此,您可以例如通知用户其他人进行了更改并刷新其显示或合并更改并保存合并的数据。

Have a look at entity framework. 看一下实体框架。 There are literally loads of tutorials and examples available for you. 从字面上看,您可以找到大量的教程和示例。 This should get you started. 这应该使您入门。

http://www.asp.net/entity-framework http://www.asp.net/entity-framework

http://msdn.microsoft.com/en-us/library/bb386876.aspx http://msdn.microsoft.com/en-us/library/bb386876.aspx

This specifically references data concurrency (its MVC but the principles are the same) 这专门引用了数据并发(其MVC,但原理相同)

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application

using (var transaction = connection.BeginTransaction())
{

  adapter.Update(dataSet); 

  transaction.Commit();
}

If the update is to a small number of rows within a table, SQL server grants a row level lock to the caller. 如果更新是针对表中的少量行,则SQL Server会将行级锁授予调用方。 If the change is to a large number of rows, SQL server grants a table level lock. 如果更改为大量行,则SQL Server授予表级锁定。 Its all automatic. 它是完全自动的。 Hence concurrency is taken care of. 因此并发得到照顾。

The problem however is that with many users simultaneously working on the same set of rows, chance of a dead lock are high. 但是,问题在于,由于许多用户同时在同一组行上工作,因此死锁的可能性很高。 The new CQRS design pattern promoted by Udi Dahan takes care of that. 乌迪·达汉(Udi Dahan)倡导的新CQRS设计模式解决了这一问题。 How ever if your application is small, applying CQRS would be an overkill. 但是,如果您的应用程序很小,那么应用CQRS可能会过大。

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

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