一個不簡單的問題,高手請進!!!!

yezhiyuan1303 2003-08-20 06:10:21
我用ADO連接SQLSERVER2000的數據庫,我如何做?才能在我編輯一條數據記錄時,
不准別人讀這條數據記錄,也就是如何才能鎖定這條記錄,當數據鎖定時,又如何知道數據被鎖定了.
...全文
22 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yezhiyuan1303 2003-08-21
  • 打赏
  • 举报
回复
能不能詳細介紹一下鎖的原理呀,
還有我在兩個程序中用ADO控件同時編輯同一個表時在保存時會出錯呀,提示信息時這樣的
Key column information is insufficient or incorrect, Too many rows were offected
by update
這樣的問題應該怎麼解決呀,在不同的程序中怎麼樣才能知道數據表當前處于編輯狀態呀?
pengdali 2003-08-20
  • 打赏
  • 举报
回复
设table1(A,B,C)
A B C
a1 b1 c1
a2 b2 c2
a3 b3 c3

1)排它锁
新建两个连接
在第一个连接中执行以下语句
begin tran
update table1
set A='aa'
where B='b2'
waitfor delay '00:00:30' --等待30秒
commit tran
在第二个连接中执行以下语句
begin tran
select * from table1
where B='b2'
commit tran

若同时执行上述两个语句,则select查询必须等待update执行完毕才能执行即要等待30秒

2)共享锁
在第一个连接中执行以下语句
begin tran
select * from table1 holdlock -holdlock人为加锁
where B='b2'
waitfor delay '00:00:30' --等待30秒
commit tran

在第二个连接中执行以下语句
begin tran
select A,C from table1
where B='b2'
update table1
set A='aa'
where B='b2'
commit tran

若同时执行上述两个语句,则第二个连接中的select查询可以执行
而update必须等待第一个连接中的共享锁结束后才能执行 即要等待30秒

3)死锁
增设table2(D,E)
D E
d1 e1
d2 e2
在第一个连接中执行以下语句
begin tran
update table1
set A='aa'
where B='b2'
waitfor delay '00:00:30'
update table2
set D='d5'
where E='e1'
commit tran

在第二个连接中执行以下语句
begin tran
update table2
set D='d5'
where E='e1'
waitfor delay '00:00:10'
update table1
set A='aa'
where B='b2'
commit tran

同时执行,系统会检测出死锁,并中止进程

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧