我有一个名为“帖子”的数据库表,它存储有关网站上一篇文章提交的所有信息。有一个名为“Views”的列,每次查看特定帖子时该值都会增加。
过程是这样的:
- 从数据库中获取记录
- 将当前值加一
- 将更改保存到数据库。
非常简单。我担心的是,如果多人同时单击该链接,更新将不准确。我应该如何处理这个问题?这应该只在存储过程中完成吗?
/// <summary>
/// Updates the view count of a post.
/// </summary>
/// <param name="postId">The Id of the post to update</param>
public bool UpdateViewCount(int postId)
{
Repository repository = new Repository();
Post p = repository.Posts.Where(p => p.Id == postId).SingleOrDefault();
if (p != null)
{
p.Views++;
}
repository.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
}
最佳答案
一次完成:
UPDATE table SET views=views+1 WHERE myId=12;
关于c# - 更新数据库中网页的查看次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/376502/