如何在 C# 中执行 SQL Analysis Server 的 MDX 查询
How to execute a MDX query of SQL Analysis Server in C#
我想在 C# 中执行 SQL 分析查询。我已使用以下代码成功连接到分析数据库:
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];
我有一个 SQL 查询
SELECT FLATTENED PredictAssociation()
From
[Mining Structure Name]
NATURAL PREDICTION JOIN
(SELECT (SELECT 1 AS [UserId]) AS [Vm]) AS t
您需要使用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();
在代码示例中,我从问题中借用了 DMX 查询。但任何 MDX 或 DAX 查询也应该有效。
更新:.NET Core 的 ADOMD.NET (Microsoft.AnalysisServices.AdomdClient) 现在在 nuget 中 available。这是 Microsoft 维护的官方库。
我想在 C# 中执行 SQL 分析查询。我已使用以下代码成功连接到分析数据库:
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];
我有一个 SQL 查询
SELECT FLATTENED PredictAssociation()
From
[Mining Structure Name]
NATURAL PREDICTION JOIN
(SELECT (SELECT 1 AS [UserId]) AS [Vm]) AS t
您需要使用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();
在代码示例中,我从问题中借用了 DMX 查询。但任何 MDX 或 DAX 查询也应该有效。
更新:.NET Core 的 ADOMD.NET (Microsoft.AnalysisServices.AdomdClient) 现在在 nuget 中 available。这是 Microsoft 维护的官方库。