The suggested duplicate doesn't answer the question in the title.
I want to execute two MSSQL commands without any other "user" (I'm not sure what's the correct term) executing a command between them.
Searching, I have found two ways that seem like they would achieve that but am unsure about them:
Use TABLOCK. - But I've seen it being considered bad practice.
Use a transaction - But all I could find was that it will be atomic, and not necessarily locking out other actions.
What way is the correct way?
More info: Only my program will be accessing the database, but it might be from several instances of it, and I don't mind a short wait - if one instance will have to wait in line for a second or two - that's fine.
EDIT: I'm trying to insert a row and get its identity. (It seems not to be straightforward as I would expect.)