如何在sqlserver 2005中锁定记录 [英] How to lock a record in sqlserver 2005

查看:127
本文介绍了如何在sqlserver 2005中锁定记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何锁定sqlserver 2005中的记录

=============================== ======



嗨朋友们,



我想锁定sqlserver中的记录,所以其他用户无法改变它。



要求:

===========

我在SQL中有一个名为tbl_SerialNoGenerator的表

这个我有列名(SerialNo),其序列号从1001到50000



在asp.net网页中,我有一个带有label,textboxes和btn的表单,它生成了这个serialNo。使用标签。



在每个页面加载时它会生成serialno。来自数据库并贴上标签。

所以没有。用户使用不同的表格。



直到这里工作正常。



所以我需要帮助,我想锁定记​​录。所以其他用户不会生成相同的serialno。



请帮帮我,如何用SQL锁定记录。



谢谢。

How to lock a record in sqlserver 2005
=====================================

Hi Friends,

I want to lock the record in sqlserver, so other users cannot change it.

Requirement:
===========
I have a table in SQL with name tbl_SerialNoGenerator
in this i have columnname(SerialNo) which has serialnos from 1001 to 50000

In asp.net webpage i have form with label, textboxes and btn which generates this serialNo. using label.

On every pageload it generates serialno. from database and puts on label.
So no. of users uses different forms.

Till here its working fine.

So I need help here, I want to Lock to record. so other users will not generate same serialno.

Please help me, how to lock the record in SQL.

THANKS.

推荐答案

您必须手动执行此操作,因为您无法使用SQL锁。原因是当您请求页面服务器获取sql数据并将其发送到客户端然后断开连接,因此您不再与sql连接,因此无法使用sql锁。



您需要实现自己的锁定。向表中添加一个字段以指示当前正在编辑记录的人员。然后在你自己的逻辑中,当别人试图访问相同的记录时,你会相应地处理它。



但是,由于这是一个浏览器应用程序,没有完美的方法要确定浏览器已经关闭,所以你需要运行一些清理代码,以确保在锁定记录后有人关闭浏览器时,锁不会永久保留。
You'll have to do this manually because you cannot use SQL locks. The reason is when you request the page the server gets the sql data and send it to the client and then disconnects so you no longer have a connection to sql and therefore cannot use sql locks.

You need to implement your own locking. Add a field to the table to indicate who is currently editing the record. Then in your own logic, when someone else tries to access the same record you handle it accordingly.

However, since this is a browser application, there is no perfect way to know for sure that the browser has been closed so you'll want to run some cleanup code to make sure locks don't stay around permanently in case someone closes the browser after locking a record.


这篇关于如何在sqlserver 2005中锁定记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆