Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi How can I lock some special records in a table in sqlserver to prevent modifying whit c# code,
thanks in advance.
Posted
Comments
BharatPrajapati212 17-Sep-14 3:28am    
simple just make the trigger for update for preventing update in table for your special record

1 solution

SQL does not support explicit lock using some commands (all the locks are done by the server according the needs of the running command)...
The best thing you can do is apply triggers on DML events and cancel changes via those triggers... http://technet.microsoft.com/en-us/library/ms191524(v=sql.105).aspx[^]
 
Share this answer
 
Comments
Angela 10293848 17-Sep-14 4:30am    
my sql works in local networks and some user can update it,I want to when a user in updating a record other user not be able to modify it I used the following code but it lock all recoreds not just a recored
begin tran select * from Table_AR_Blok WITH (HOLDLOCK, ROWLOCK) where Id='" + TId.Text.Trim() + "'
Kornfeld Eliyahu Peter 17-Sep-14 4:33am    
I do not understand you. What do you mean by 'when user updating'? At that very moment?
You additions to the select statement are only hints to the query optimizer and has no effect of locking anything outside the select itself...(and maybe not even inside)...
Angela 10293848 17-Sep-14 4:40am    
I have a button to select when I click this button, data are shown in textboxes and I change the data then I update these data with another button I mean that I do not want to allow other user be able to change this record in this duration(from select up to click second button)
Kornfeld Eliyahu Peter 17-Sep-14 4:46am    
This called 'concurrency control' and can not be done in pure SQL, you need some help from the application...
You may read here: http://publib.boulder.ibm.com/infocenter/soliddb/v6r3/index.jsp?topic=/com.ibm.swg.im.soliddb.sql.doc/doc/the.purpose.of.concurrency.control.html
Angela 10293848 17-Sep-14 5:07am    
ok but the question is when I lock Id=1 it locks all records Id=1,2,3,
why?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900