2011-03-04 42 views
1

我們擁有由各個員工維護的一組SSAS 2005數據庫。包括格式字符串等的元數據/模式已經演變爲代表大量的工作,並且它們週期性地改變。我們試圖在商業智能項目的源代碼控制下進行項目,但是如果沒有數據本身,每晚都要備份所有SSAS元數據也是一件好事。 (該數據當然是巨大的和可重複的,而模式是微小的。)SSAS以編程方式僅備份元數據

我可以用Microsoft.AnalysisServices.Server.Databases集合輕鬆地以編程方式(C#)遍歷所有SSAS dbs,但我沒有找到了一種簡單的方法來備份沒有數據的模式。使用SSMS,我可以右鍵單擊數據庫並選擇[Script Database as] - > [CREATE To] - > [File ...]作爲示例,並獲取表示整個數據庫元數據的XMLA。這在這裏被引用:http://msdn.microsoft.com/en-us/library/ms174589.aspx,我相信這有我們想要備份的所有信息...但是我沒有找到在Microsoft.AnalysisServices程序集中提供類似功能的方法,我不確定在哪裏可以看到。

回答

2

我知道我可能是有點晚了,但在這裏不用..

這是PowerShell的,但轉換成C#是一件輕而易舉的事:

$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() 

它不是我的,我發現它here其中Darren Gosbell已發佈。

+0

感謝泰勒(和Darren)! – Penny 2012-01-05 14:19:16

+1

上述工作適用於多昏暗的立方體。對於表格模型,您應該使用Microsoft.AnalysisServices.Tabular.JsonScripter – Fai 2017-06-15 22:00:45

2

我已經找到了解決辦法:

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(); 
    } 
} 
相關問題