I want to execute a SQL Analysis Query in C#. I have successfully connected to Analysis database using the below code:
Server DM_Server = new Server();
Database AS_Database = new Database();
DM_Server.Connect(//SQL Analysis Server Connection String);
AS_Database = DM_Server.Databases[//Database name];
I have a SQL query like
SELECT FLATTENED PredictAssociation()
From
[Mining Structure Name]
NATURAL PREDICTION JOIN
(SELECT (SELECT 1 AS [UserId]) AS [Vm]) AS t
You need to use ADOMD.NET which is the Microsoft.AnalysisServices.AdomdClient namespace. It is also available on Nuget.
AdomdConnection conn = new AdomdConnection(
"Data Source=localhost;Catalog=YourDatabase");
conn.Open();
string commandText = @"SELECT FLATTENED
PredictAssociation()
From
[Mining Structure Name]
NATURAL PREDICTION JOIN
(SELECT (SELECT 1 AS [UserId]) AS [Vm]) AS t ";
AdomdCommand cmd = new AdomdCommand(commandText, conn);
AdomdDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
Console.WriteLine(Convert.ToString(dr[0]));
}
dr.Close();
conn.Close();
In the code sample I borrowed the DMX query from the question. But any MDX or DAX query should also work.
Update: ADOMD.NET (Microsoft.AnalysisServices.AdomdClient) for .NET Core is available in on nuget now. This is an official Microsoft maintained library.