Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting single user mode to restore backup

I have the following function:

public void RestoreDatabase(String databaseName, String backUpFile, 
                            String serverName, String userName, String password)
{
    SqlConnection.ClearAllPools();
    ServerConnection connection = new ServerConnection
                                          (serverName, userName, password);
    Server sqlServer = new Server(connection);
    Restore rstDatabase = new Restore();
    rstDatabase.Action = RestoreActionType.Database;
    rstDatabase.Database = databaseName;
    BackupDeviceItem bkpDevice = new BackupDeviceItem
                                         (backUpFile, DeviceType.File);
    rstDatabase.Devices.Add(bkpDevice);
    rstDatabase.ReplaceDatabase = true;
    rstDatabase.SqlRestore(sqlServer);
}

I was trying to set the DB to single user mode first, before restoring the backup. I tried this code:

        private string singleUserCmd = "alter database db-name set SINGLE_USER";
        private string multiUserCmd = "alter database db-name  set MULTI_USER";

        private void SetSingleUser(bool singleUser, 
                                   SqlConnectionStringBuilder csb)
        {
            string v;
            if (singleUser)
            {
                v = singleUserCmd.Replace("db-name", csb.InitialCatalog);
            }
            else
            {
                v = multiUserCmd.Replace("db-name", csb.InitialCatalog);
            }
            SqlCommand cmd = new SqlCommand(v, new SqlConnection
                                                     (csb.ToString()));
            try
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
            }
            finally
            {
                cmd.Connection.Close();
            }
        }

The problem seems to be that single user mode is for that connection, preventing anyone else from doing anything. I need it to be for the connection that restores the backup.

like image 870
Anthony D Avatar asked Nov 04 '09 14:11

Anthony D


People also ask

Why is database in single user mode?

A single-user mode specifies that only one user at a time can access the database. If you set the database to single-user mode then all other connections will be closed without warning. If you get disconnected after setting the database into single-user mode, a different user can connect to database, but only one user.

How can I tell if a database is in single user mode?

Right-click the database to change, and then select Properties. In the Database Properties dialog box, select the Options page. From the Restrict Access option, select Single. If other users are connected to the database, an Open Connections message will appear.

How do I restore a SQL Server database backup?

Open Microsoft SQL Server Management Studio. In the left navigation bar, right-click on Databases and then click Restore Database. In the Source section, select Device and click the button with three dots. In the pop up window that opens, click Add and browse for your backup file.


1 Answers

The restore would need to occur on the connection which placed the DB into single user mode, so why not make your SetSingleUser function return the opened SqlConnection it executed on and then have your Restore code take in and use the same opened connection.

private string singleUserCmd = "alter database db-name set SINGLE_USER";
private string multiUserCmd = "alter database db-name  set MULTI_USER";

private SqlConnection SetSingleUser(bool singleUser, SqlConnectionStringBuilder csb)
{
    string v;
    if (singleUser)
    {
        v = singleUserCmd.Replace("db-name", csb.InitialCatalog);
    }
    else
    {
        v = multiUserCmd.Replace("db-name", csb.InitialCatalog);
    }
    SqlConnection connection = new SqlConnection(csb.ToString());
    SqlCommand cmd = new SqlCommand(v, connection);

        cmd.Connection.Open();
        cmd.ExecuteNonQuery();

    return connection;
}
like image 175
John Lemp Avatar answered Nov 03 '22 22:11

John Lemp