开发者

Is there a way to set the DB as Single User Mode in C#?

开发者 https://www.devze.com 2022-12-08 06:17 出处:网络
I have a WPF app that updates my database from an in-code entity model. There may be instances that it updates the DB while there are users connected and I would like to put it in Single-User-Mode to

I have a WPF app that updates my database from an in-code entity model. There may be instances that it updates the DB while there are users connected and I would like to put it in Single-User-Mode to avoid errors.

I would also like to avoid using sql. I am aware that I can run sql using:

DataContext.ExecuteCommand("ALTER DATABASE...") 

I would rather use a command in a C# 开发者_开发百科library to do this but I dont know where to look.

Is there a way to set the DB to SUM without using SQL?


So I used the SMO server objects like this:

Server server = GetServer();
         if (server != null)
        {
            Database db = server.Databases[Settings.Instance.GetSetting("Database", "MyDB")];
            if (db != null)
            {
                server.KillAllProcesses(db.Name);
                db.DatabaseOptions.UserAccess = DatabaseUserAccess.Single;
                db.Alter(TerminationClause.RollbackTransactionsImmediately);

The problem was that this opened a different connection than my datacontext thus kicking myself off and not letting me access the DB. In the end I had to revert to using SQL.


You can use SQLDMO to manage SQL Server objects.

http://msdn.microsoft.com/en-us/library/ms162169.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消