如何锁定SQL Server数据库以进行更改?

时间:2022-05-04 14:03:12

I want to make sure my database (SQL Server 2008) remains immutable, but still accessible for reads during some maintenance operations (backups, transitions to other DB servers etc), how can I programmatically lock it for changes in C#?

我想确保我的数据库(SQL Server 2008)保持不变,但在某些维护操作(备份,转换到其他数据库服务器等)期间仍然可以读取,我如何以编程方式锁定C#中的更改?

1 个解决方案

#1


12  

You can execute an ALTER DATABASE <mydb> SET READ_ONLY to put a database in read-only mode.

您可以执行ALTER DATABASE SET READ_ONLY以将数据库置于只读模式。

If you want the command to fail if it can't be executed immediately you specify it like this:

如果您希望命令在无法立即执行时失败,请按以下方式指定:

    ALTER DATABASE <mydb> 
    SET READ_ONLY
    WITH NO_WAIT

If you want to make it rollback all open connections you specify the command like this:

如果要使其回滚所有打开的连接,请指定以下命令:

    ALTER DATABASE <mydb> 
    SET READ_ONLY
    WITH ROLLBACK IMMEDIATE

These options are well documented in SQL Server Books online

这些选项在SQL Server联机丛书中有详细记录

#1


12  

You can execute an ALTER DATABASE <mydb> SET READ_ONLY to put a database in read-only mode.

您可以执行ALTER DATABASE SET READ_ONLY以将数据库置于只读模式。

If you want the command to fail if it can't be executed immediately you specify it like this:

如果您希望命令在无法立即执行时失败,请按以下方式指定:

    ALTER DATABASE <mydb> 
    SET READ_ONLY
    WITH NO_WAIT

If you want to make it rollback all open connections you specify the command like this:

如果要使其回滚所有打开的连接,请指定以下命令:

    ALTER DATABASE <mydb> 
    SET READ_ONLY
    WITH ROLLBACK IMMEDIATE

These options are well documented in SQL Server Books online

这些选项在SQL Server联机丛书中有详细记录