Programmatically switching database to read-only and write on SQL Server

寵の児 提交于 2020-01-17 07:40:31

问题


Can you programmatically switch a database to write access and back to read-only in C#?


回答1:


Reference

ALTER DATABASE database-name SET READ_ONLY

ALTER DATABASE database-name SET READ_WRITE

You can access the stored procedure in c# like below to execute the above lines.

using (System.Data.SqlClient.SqlConnection con = new SqlConnection("YourConnection string")) {
    con.Open();
    SqlCommand cmd = new SqlCommand();
    string expression = "Parameter value";
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "Your Stored Procedure";
    cmd.Parameters.Add("Your Parameter Name", SqlDbType.VarChar).Value = expression;
    cmd.Connection = con;
    using (IDataReader dr = cmd.ExecuteReader()) {
        if (dr.Read()) {
        }
    }
}


来源:https://stackoverflow.com/questions/9026456/programmatically-switching-database-to-read-only-and-write-on-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!