0

I'm looking for an automatic method for generating the file that is created in the first screenshot to see if it can be done:

Manual method for generating file in SSMS

It is an xmla file detailing the structure of the tables within the database. Preferably a solution in C# or PowerShell, but any method would be appreciated.

The second image is the format of the desired output file. This is easily generated from SSMS but I would like to run a script to generate this instead:

Desired xmla output file

3
  • What have you tried so far? What does the xmla file contain if you try to open it with a text editor? XML? SQL? Commented Jul 22, 2021 at 9:33
  • Does this answer in StackExchange help? dba.stackexchange.com/questions/51129/… Commented Jul 22, 2021 at 9:38
  • I've attatched above the xmla file that I am looking for. Commented Jul 22, 2021 at 11:53

1 Answer 1

1

If you are using SQL Server Analysis Services database:

In C# you can use Microsoft.AnalysisServices.retail.amd64 package as follow:

Server yourServer = new Server();
yourServer.Connect(@"Data Source=ServerName\InstanceName");

MajorObject[] yourDatabase = new MajorObject[1];
yourDatabase[0] = yourServer.Databases["DbName"];

var scripter = new Scripter();
XmlWriter xmlWriter = XmlWriter.Create(@"FilePath")

Take a look at the following link. The question is similar to your one:

Need c# script for SMO which creates XMLA script from analysis service database


If you are using SQL Server database:

In C# you can use Microsoft.SqlServer.SqlManagementObjects package to generate script for database objects as follow:

SQL Server Management Objects (SMO) Programming Guide

Server yourServer = new Server("ServerName");

Scripter scripter = new Scripter(yourServer);

Database yourDb = yourServer.Databases["DbName"];

foreach (Table table in yourDb.Tables)
{
    tableScripts = table.Script();
    foreach (string script in tableScripts)
        Console.WriteLine(script);
}

You can also use scripting options to IfNotExists , ScriptDrops, and so on. I suggest you to take a look at the following links:

Generate Scripts for database objects with SMO for SQL Server

Scripting Database Objects using SMO

And then, you can create your script file using C# code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.