To prevent the same data being INSERTed at the same time, use a unique index on the columns that you want to be unique. The first INSERT
will succeed, handle the error for the losing INSERT
appropriately.
To prevent 2+ users from modifying the same (existing) record at the same time, use optimistic concurrency http://en.wikipedia.org/wiki/Optimistic_concurrency_control. With SQL Server it's easy to implement optimistic concurrency using a TIMESTAMP
column. Read the timestamp with your data. When updating the row, check that the timestamp value is the same. If the timestamps don't match, then the user had (was viewing) an outdated row. Handle that case appropriately.
An example using SqlClient:
command.CommandText = @"
UPDATE tbl
SET LastName = @LastName, FirstName = @FirstName
WHERE ID = @ID AND Timestamp = @Timestamp
";
int rowCount = command.ExecuteNonQuery();
if (rowCount != 1)
throw new DBConcurrencyException();