Search code examples
c#sql-serverpowershellxmla

.xmla deployment to SQL using C#


I use Invoke-ASCmd in PowerShell right now to create a database in SQL Server, like this:

Invoke-ascmd -Query $MyScript -Server $ASServer

Where $MyScript is a string holding the contents of an .xmla file that I read in previously.

This works great. Now I need to do something similar in C#, but am unable to find a simple solution like the one that exists in PowerShell.

I see some people using a Microsoft DLL called Microsoft.AnalysisServices.XMLA.dll, but it's not supported, and the class in question is "internal", so I can't even reference it.

I found this DLL Microsoft.AnalysisServices.AdomdClient.dll while searching around, but don't see any of the classes being relevant to what I need: https://learn.microsoft.com/en-us/dotnet/api/microsoft.analysisservices.adomdclient?view=analysisservices-dotnet


Solution

  • using Microsoft.AnalysisServices.AdomdClient;
    
    try
    {
        var xmlaFileContents = File.ReadAllText("path/to/your/file.xmla");
    
        using (AdomdCommand cmd = conn.CreateCommand())
        {
            cmd.CommandText = xmlaFileContents;
            cmd.ExecuteNoQuery();
        }
    }
    catch(Exception)
    {
    }
    

    ** please note that I have not run this code **