问题
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