问题
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
回答1:
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();
来源:https://stackoverflow.com/questions/43436925/how-to-execute-a-mdx-query-of-sql-analysis-server-in-c-sharp