有没有办法在C#中将数据库设置为单用户模式? [英] Is there a way to set the DB as Single User Mode in C#?

查看:125
本文介绍了有没有办法在C#中将数据库设置为单用户模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序从代码中实体模型更新我的数据库。可能有一些实例,当有用户连接时,它更新数据库,我想把它放在单用户模式,以避免错误。

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.

我也想避免使用sql。我知道我可以运行sql使用:

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

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

我宁愿在C#库中使用命令,但我不知道在哪里看。

I would rather use a command in a C# library to do this but I dont know where to look.

有没有办法将数据库设置为SUM而不使用SQL?

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

推荐答案

所以我使用了这样的SMO服务器对象:

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);

问题是,这打开了一个不同于我的数据文件的连接,从而使我自己关闭,数据库。最后我不得不恢复使用SQL。

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.

这篇关于有没有办法在C#中将数据库设置为单用户模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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