Switching an Analysis Services database between ReadOnly and ReadWrite modes
There are often situations when a Analysis Services database administrator (dba) wants to change the read/write mode of a database. These situations are often driven by business needs, such as sharing the database among a pool of Analysis Services servers for a better user experience.
A database mode can be switched in many ways. This document explains the following common scenarios:
Interactively using SQL Server Management Studio
Programmatically using AMO
By script using XMLA
Procedures
To switch the read/write mode of a database interactively using Management Studio
Locate the database to be switched in the left or right pane of Management Studio.
Right-click the database and select Properties. Find the database folder and note the location. An empty database storage location indicates that the database folder is located in the server data folder.
Important
As soon as the database is detached, Management Studio can no longer help you obtain the database location.
Right-click the database and select Detach…
Assign a password to the database to be detached, and then click OK to execute the detach command.
Locate the Databases folder in the left or right pane of Management Studio.
Right-click the Databases folder and select Attach…
In the folder text box, type the original location of the database folder. Alternatively, you can use the browse button (…) to locate the database folder.
Select the read/write mode for the database.
Type the password that was used in step 3 and click OK to execute the attach command.
To switch the read/write mode to a database programmatically using AMO
- In your C# application, adapt the following sample code and complete the indicated tasks.
private void SwitchReadWrite(Server server, string dbName,
ReadWriteMode dbReadWriteMode)
{
if (server.Databases.ContainsName(dbName))
{
Database db;
string databaseLocation;
db = server.Databases[dbName];
databaseLocation = db.DbStorageLocation;
if (databaseLocation == null)
{
string dataDir = server.ServerProperties["DataDir"].Value;
String[] possibleFolders = Directory.GetDirectories(dataDir, string.Concat(dbName,"*"), SearchOption.TopDirectoryOnly);
if (possibleFolders.Length > 1)
{
List<String> sortedFolders = new List<string>(possibleFolders.Length);
sortedFolders.AddRange(possibleFolders);
sortedFolders.Sort();
databaseLocation = sortedFolders[sortedFolders.Count - 1];
}
else
{
databaseLocation = possibleFolders[0];
}
}
db.Detach();
server.Attach(databaseLocation, dbReadWriteMode);
}
}
In your C# application, invoke SwitchReadWrite() with the necessary parameters.
Compile and execute your code to move the database.
To switch the read/write mode to a database by script using XMLA
Locate the database to be switched in the left or right pane of Management Studio.
Right-click the database and select Properties. Find the database folder and note the location. An empty database storage location indicates that the database folder is located in the server data folder.
Important
As soon as the database is detached, Management Studio can no longer help you obtain the database location.
Open a new XMLA tab in Management Studio.
Copy the following script template for XMLA:
<Detach xmlns="https://schemas.microsoft.com/analysisservices/2003/engine">
<Object>
<DatabaseID>%dbName%</DatabaseID>
<Password>%password%</Password>
</Object>
</Detach>
Replace %dbName% with the name of the database and %password% with the password. The % characters are part of the template and must be removed.
Execute the XMLA command.
Copy the following script template for XMLA in a new XMLA tab
<Attach xmlns="https://schemas.microsoft.com/analysisservices/2003/engine">
<Folder>%dbFolder%</Folder>
<ReadWriteMode xmlns="https://schemas.microsoft.com/analysisservices/2008/engine/100">%ReadOnlyMode%</ReadWriteMode>
</Attach>
Replace %dbFolder% with the complete UNC path of the database folder, %ReadOnlyMode% with the corresponding value ReadOnly or ReadWrite, and %password% with the password. The % characters are part of the template and must be removed.
Execute the XMLA command.
See Also
Reference
Attach()