views:

104

answers:

2

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?

+1  A: 

You can use SQLDMO to manage SQL Server objects.

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

Raj More
A: 

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.

johnnywhoop