简体   繁体   中英

How to process an SSAS Tabular database from C#?

I want to process a Tabular database on SSAS server from C#.

My code is as follows:

using (var server = new Microsoft.AnalysisServices.Tabular.Server())
{
    server.Connect("Data source = localhost");
    var db = server.Databases["MyModel"];

    // this line throws
    db.Process();
}

The last line throws an exception:

Microsoft.AnalysisServices.OperationException: 'This command cannot be executed on database 'MyModel_SingleTable_2' because it has been defined with StorageEngineUsed set to TabularMetadata. For databases in this mode, you must use Tabular APIs to administer the database.

Given the wording of the message, it looks like I'm using the wrong method. What is the correct way to process a tabular model from C#?

I can process the database just fine from SSMS.

It turns out the correct way of doing it to call RequestRefresh and then call SaveChanges . This must be done in a transaction.

Also, the SaveChanges method returns a result that allows inspecting error messages and warnings.

Here is the final code snippet:

using (var server = new Microsoft.AnalysisServices.Tabular.Server())
{
    server.Connect("Data source = localhost");
    var db = server.Databases["MyModel"];

    // use this instead of db.Process()
    server.BeginTransaction();
    db.Model.RequestRefresh(RefreshType.Full);
    var saveChangesResult = db.Model.SaveChanges(new SaveOptions() { SaveFlags = SaveFlags.ForceValidation });
    server.CommitTransaction();

    // inspect error messages and warnings
    var messages = saveChangesResult
        .XmlaResults.OfType<Microsoft.AnalysisServices.XmlaResult>()
        .SelectMany(r => r.Messages.OfType<Microsoft.AnalysisServices.XmlaMessage>());

    foreach (var msg in messages)
    {
        // log warnings and errors
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM