Search code examples
sql-serversql-server-2008database-restoreservice-broker

Enabling broker after Restoring Sql Server DataBase


I have DataBase with enabled Service Broker. Then I want to restore my database in program from backup of other database, but after restoring(I restore on existing database name), my method, whitch enables Service Broker, puts this error:

    Msg 9772, Level 16, State 1, Line 1
The Service Broker in database "ServeDB2" cannot be enabled because there is already an enabled Service Broker with the same ID.
Msg 5069, Level 16, State 1, Line 1
ALTER DATABASE statement failed.

This is my method:

public void TurnOnBroker()
{
    if (!this.database.BrokerEnabled)
    {
        this.server.KillAllProcesses(this.database.Name);
        this.database.BrokerEnabled = true;
        this.database.Alter();
        RefreshConnection();
    }
}

What should i fix here?Any suggestions?


Solution

  • I found a very simple solution for that- just simlpy assign new service broker, like this:

    public void TurnOnBroker()
        {
            if (!this.database.BrokerEnabled)
            {
                this.server.KillAllProcesses(this.database.Name);
    
                string brokerCommand = String.Format("ALTER DATABASE {0} SET NEW_BROKER", this.database.Name);
                this.database.ExecuteNonQuery(brokerCommand);
    
                RefreshConnection();
            }
        }