以编程方式将数据库切换为只读并在 SQL Server 上写入 [英] Programmatically switching database to read-only and write on SQL Server

查看:58
本文介绍了以编程方式将数据库切换为只读并在 SQL Server 上写入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否以编程方式将数据库切换为 C# 中的写访问权限和只读访问权限?

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

推荐答案

参考资料

ALTER DATABASE database-name SET READ_ONLY

ALTER DATABASE database-name SET READ_WRITE

您可以像下面那样在 c# 中访问存储过程以执行以上几行.

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()) {
        }
    }
}

这篇关于以编程方式将数据库切换为只读并在 SQL Server 上写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆