We have a set of SSAS 2005 databases that are maintained by various employees. The metadata/schema including format strings, etc. have evolved to represent a significan开发者_如何学Got amount of work and they change periodically. We've tried having the business intelligence projects under source control, but it would also be nice to have a nightly backup of all SSAS metadata, without the data itself. (The data is of course huge and reproducible, whereas the schema is tiny.)
I can programmatically (C#) loop through all the SSAS dbs easily with the Microsoft.AnalysisServices.Server.Databases collection, but I haven't found a simple way to back up the schema without the data. Using SSMS, I can right-click the db and choose [Script Database as]-->[CREATE To]-->[File ...] for example and get XMLA representing the entire database metadata. This is referenced here: http://msdn.microsoft.com/en-us/library/ms174589.aspx, and I believe this has all the information we'd like to back up... however I haven't found methods providing similar functionality in the Microsoft.AnalysisServices assembly and am not sure where else to look.
I know I may be kind of late, but here it goes..
This is PowerShell, but converting to C# is a breeze:
$svrName = "localhost\sql08"
$sourceDB = "Adventure Works DW 2008"
$sourceCube = "Adventure Works"
# load the AMO library (redirect to null to 'eat' the output from assembly loading process)
[System.Reflection.Assembly]::LoadwithpartialName("Microsoft.AnalysisServices") > $null
# connect to the AS Server
$svr = New-Object Microsoft.AnalysisServices.Server
$svr.Connect($svrName)
# get a reference to the database
$db= $svr.Databases.Item($sourceDB)
# get a reference to the cube
$cub = $db.Cubes.FindByName($sourceCube)
# setup the scripter object
$sb = new-Object System.Text.StringBuilder
$sw = new-Object System.IO.StringWriter($sb)
$xmlOut = New-Object System.Xml.XmlTextWriter($sw)
$xmlOut.Formatting = [System.Xml.Formatting]::Indented
$scr = New-Object Microsoft.AnalysisServices.Scripter
# create an array of MajorObjects to pass to the scripter
$x = [Microsoft.AnalysisServices.MajorObject[]] @($cub)
$scr.ScriptCreate($x,$xmlOut,$false)
$sb.ToString() > c:\data\tmpCube2.xmla
# clean up any disposeable objects
$sw.Close()
$svr.Disconnect()
$svr.Dispose()
It's not mine, I found it here where Darren Gosbell posted it.
I've found a solution:
void ScriptDb(string svrName, string dbName, string outFolderPath)
{
using (var svr = new Server())
{
svr.Connect(svrName);
// get a reference to the database
var db = svr.Databases[dbName];
// setup the scripter object
var sw = new StringWriter();
var xmlOut = new XmlTextWriter(sw);
xmlOut.Formatting = Formatting.Indented;
var scr = new Scripter();
// create an array of MajorObjects to pass to the scripter
var x = new MajorObject[] { db };
scr.ScriptCreate(x, xmlOut, true);
// todo: would be wise to replace illegal filesystem chars in dbName
var outPath = Path.Combine(outFolderPath, dbName + ".xmla");
File.WriteAllText(outPath, sw.ToString());
// clean up any disposeable objects
sw.Close();
svr.Disconnect();
svr.Dispose();
}
}
精彩评论