问题描述
我正在构建一个.NET客户端应用程序(C#,WinForms),它使用Web服务与数据库进行交互。客户端将使用WAN或VPN从远程位置运行,因此使用Web服务而不是直接访问数据库的想法。
I'm building a .NET client application (C#, WinForms) that uses a web service for interaction with the database. The client will be run from remote locations using a WAN or VPN, hence the idea of using a web service rather than direct database access.
我遇到的问题现在是如何处理数据库并发。也就是说,如果两个人从不同的位置更新相同的数据,我该如何处理呢?我正在考虑对每个数据库记录使用时间戳,并将其作为更新where子句的一部分,但这意味着时间戳必须通过Web服务接口来回移动,这看起来很丑陋。
The issue I'm grappling with right now is how to handle database concurrency. That is, if two people from different locations update the same data, how do I deal with it? I'm considering using timestamps on each database record and having that as part of the update where clauses, but that means that the timestamps have to move back and forth through the web service interface, which seems kind of ugly.
这是最好的方法是什么?
What is the best way to approach this?
推荐答案
希望您的Web服务直接与数据库交谈。您可能希望您的服务与某些类型的业务组件进行交互,这些组件又与数据访问层交互。任何并发异常都可以从DAL传递到业务层,在那里它们可以被处理,以便Web服务不必看到时间戳。
I don't think you want your web service to talk directly to the database. You probably want your service to interact with some type of business components who in turn interact with a data access layer. Any concurrency exceptions can be passed from the DAL up to the business layer where they can be handled so that the web service never has to see the timestamps.
但是如果你是将类似数据表的东西传递给客户端,并且想要避免时间戳,可以通过逐字段比较来进行并发检查。如果您要求进行乐观并发检查,表适配器向导会默认生成此类型的并发检查。
But if you are passing something like a data table up to the client and you want to avoid timestamps, you can do concurrency checking by comparing field by field. The Table Adapter wizards generate this type of concurrency checking by default if you ask for optimistic concurrency checking.
这篇关于Web服务和数据库并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!