How to lock SQL Server database for changes?

I want to make sure that my database (SQL Server 2008) remains unchanged, but is still readable during some maintenance operations (backup, transitions to other database servers, etc.), how can I programmatically lock it for changes in c #?

+6
source share
1 answer

You can do ALTER DATABASE <mydb> SET READ_ONLY to put the database in read-only mode.

If you want a command to fail if it cannot be executed immediately, you specify it as follows:

  ALTER DATABASE <mydb> SET READ_ONLY WITH NO_WAIT 

If you want to disconnect all open connections, you specify the command as follows:

  ALTER DATABASE <mydb> SET READ_ONLY WITH ROLLBACK IMMEDIATE 

These options are well described in SQL Server Online documentation.

+12
source

Source: https://habr.com/ru/post/903389/


All Articles