ADO.NET interview questions: – Different ways to implement optimistic locking in ADO.NET.

This is one of the ADO.NET interview questions asked in the interview. Answer this question with the simple steps as follows.

Ways to implement optimistic locking using ADO.NET: –

  • When we call “Update” method of Data Adapter it handles locking internally. If the Dataset values are not matching with current data in Database, it raises concurrency exception error. We can easily trap this error using Try. Catch block and raise appropriate error message to the user.
  • Define a Date time stamp field in the table. When actually you are firing the UPDATE SQL statements, compare the current timestamp with one existing in the database. Below is a sample SQL which checks for timestamp before updating and any mismatch in timestamp it will not update the records. This I the best practice used by industries for locking.
Update table1 set field1=@test where Last Timestamp=@Current Timestamp
  • Check for original values stored in SQL SERVER and actual changed values. In stored procedure check before updating that the old data is same as the current Example in the below shown SQL before updating field1 we check that is the old field1 value same. If not then some one else has updated and necessary action has to be taken.
Update table1 set field1=@test where field1 = @oldfield1value

Locking can be handled at ADO.NET side or at SQL SERVER side i.e. in stored procedures: –

Also see the following video on Table Scan And Unique key in SQL Server: –

Please click here to see more ADO.NET interview questions


Visit Authors blog for more ADO.NET interview questions

About C#.NET, ASP.NET MVC Core, Angular, Azure, (MSBI)Business Intelligence, Data Science - Python Interview Questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. ASP.NET MVC Interview Questions and Answers:- C# Interview Questions and Answers:- Angular Interview Questions and Answers:- C# tutorial for beginners(4 hrs.):- Learn Azure Step by Step:- Azure AZ-900 fundamentals certification :- AZ- 204 certification Azure:- Learn Angular tutorial step by step Learn MVC 5 step by step in 16 hours:- Learn Design Pattern Step by Step Learn MSBI Step by Step in 32 hours:- Learn SQL Server Step by Step Python Tutorial for Beginners:- Learn Data Science in 1 hour :- Learn Power BI Step by Step:- Learn Tableau step by step :-
This entry was posted in Uncategorized and tagged , , , , , , , , . Bookmark the permalink.

1 Response to ADO.NET interview questions: – Different ways to implement optimistic locking in ADO.NET.

  1. Pingback: DotNetShoutout

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s