2015-02-05 72 views
0

我是新來的mdx查詢,我很好奇使用C#的mdx查詢生成,所以我搜索任何演示或開源,然後我發現Ranet.olap(https://ranetuilibraryolap.codeplex.com/)這是提供我所需要的。 服用dll之後,我嘗試將它們合併到我的代碼中。我正在粘貼我的完整的控制檯代碼,它應該會生成mdx查詢,但它並沒有這樣做,我做錯了什麼?如何使用C#生成mdx查詢?

using System; 
using System.Collections.Generic; 
using Microsoft.AnalysisServices.AdomdClient; 
using Ranet.Olap.Core.Managers; 
using Ranet.Olap.Core.Metadata; 
using Ranet.Olap.Core.Types; 

namespace MDX 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      startWork(); 
     } 

     public static void startWork() 
     { 
      string connString = "Provider=MSOLAP.3; Data Source=localhost;Initial Catalog=AdventureWorkDW2008R2;Integrated Security=SSPI;"; 
      CubeDef cubes; 
      AdomdConnection conn = new AdomdConnection(connString); 
      conn.Open(); 
      cubes = conn.Cubes.Find("AdventureWorkCube"); 

      Ranet.Olap.Core.Managers.MdxQueryBuilder mdx = new Ranet.Olap.Core.Managers.MdxQueryBuilder(); 
      mdx.Cube = cubes.Caption; 
      List<Ranet.Olap.Core.Wrappers.AreaItemWrapper> listColumn = new List<Ranet.Olap.Core.Wrappers.AreaItemWrapper>(); 
      List<Ranet.Olap.Core.Wrappers.AreaItemWrapper> listRow = new List<Ranet.Olap.Core.Wrappers.AreaItemWrapper>(); 
      List<Ranet.Olap.Core.Wrappers.AreaItemWrapper> listData = new List<Ranet.Olap.Core.Wrappers.AreaItemWrapper>(); 

      //Column area 
      Dimension dmColumn = cubes.Dimensions.Find("Dim Product"); 
      Microsoft.AnalysisServices.AdomdClient.Hierarchy hColumn = dmColumn.Hierarchies["English Product Name"]; 
      //hierarchy properties 
      List<PropertyInfo> lPropInfo = new List<PropertyInfo>(); 
      foreach (var prop in hColumn.Properties) 
      { 
       PropertyInfo p = new PropertyInfo(); 
       p.Name = prop.Name; 
       p.Value = prop.Value; 
       lPropInfo.Add(p); 
      } 
      Ranet.Olap.Core.Wrappers.AreaItemWrapper areaIColumn = new Ranet.Olap.Core.Wrappers.AreaItemWrapper(); 
      areaIColumn.AreaItemType = AreaItemWrapperType.Hierarchy_AreaItemWrapper; 
      areaIColumn.Caption = hColumn.Caption; 
      areaIColumn.CustomProperties = lPropInfo; 
      listColumn.Add(areaIColumn); 

      //Rows Area 
      Dimension dmRow = cubes.Dimensions.Find("Due Date"); 
      Microsoft.AnalysisServices.AdomdClient.Hierarchy hRow = dmRow.Hierarchies["English Month Name"]; 
      List<PropertyInfo> lRowPropInfo = new List<PropertyInfo>(); 
      foreach (var prop in hRow.Properties) 
      { 
       PropertyInfo p = new PropertyInfo(prop.Name,prop.Value); 
       lRowPropInfo.Add(p); 
      } 
      Ranet.Olap.Core.Wrappers.AreaItemWrapper areaIRow = new Ranet.Olap.Core.Wrappers.AreaItemWrapper(); 
      areaIRow.AreaItemType = AreaItemWrapperType.Hierarchy_AreaItemWrapper; 
      areaIRow.Caption = hRow.Caption; 
      areaIRow.CustomProperties = lRowPropInfo; 
      listRow.Add(areaIRow); 


      //Measure Area or Data Area 
      Measure ms = cubes.Measures.Find("Order Quantity"); 
      Ranet.Olap.Core.Wrappers.AreaItemWrapper areaIData = new Ranet.Olap.Core.Wrappers.AreaItemWrapper(); 
      areaIData.AreaItemType = AreaItemWrapperType.Measure_AreaItemWrapper; 
      areaIData.Caption = ms.Caption; 

      List<PropertyInfo> lmpropInfo = new List<PropertyInfo>(); 

      foreach (var prop in ms.Properties) 
      { 
       PropertyInfo p = new PropertyInfo(prop.Name, prop.Value); 
       lmpropInfo.Add(p); 
      } 
      areaIData.CustomProperties = lmpropInfo; 
      listData.Add(areaIData); 

      mdx.AreaWrappersColumns = listColumn; 
      mdx.AreaWrappersRows = listRow; 
      mdx.AreaWrappersData = listData; 
      string mdxQuery = mdx.GenerateMdxQuery(); 
      conn.Close(); 
     } 
    } 
} 
+0

請問您可以添加錯誤ou tput的?或者mdxquery變量值最初的錯誤是什麼? – George 2015-02-06 06:50:34

+0

它沒有給出任何錯誤。實際上查詢生成器方法在生成時不識別列,行和測量項目。的上面的代碼輸出是 「SELECT FROM [AdventureWorkCube] 電池特性BACK_COLOR,CELL_ORDINAL,FORE_COLOR,FONT_NAME,FONT_SIZE,FONT_FLAGS,FORMAT_STRING,VALUE FORMATTED_VALUE,UPDATEABLE,ACTION_TYPE 」 – 2015-02-06 11:29:26

回答

0

仍然在修改代碼,這臺發動機的過程中,雖然你一些建議:

  • 它看起來像你剛纔搶立方體元數據(變暗,措施等),並把它傳遞給發電機。這聽起來不像是生成MDX的方法。 MDX語句應該看起來像

    select 
    { 
        // measures, calculated members 
    } on 0, 
    { 
        // dimension data - sets 
    } on 1 // probably more axis 
    from **Cube** 
    

其他所有參數都是可選的

+0

cubes.Dimensions.Find( 「暗淡產品」 )返回我的維度對象,它具有像層次結構一樣的所有子元素,並且不返回null。而且我也無法找到具有簽名FindByName(字符串DimensionName)的方法; – 2015-02-06 11:48:16

+0

是的你是對的,我提到的功能只存在於使用Microsoft.AnalysisServices而不是Adomd – George 2015-02-06 12:17:18

+0

我也在我的項目中使用Microsoft.AnalysisServices.AdomdClient,但它不可用。對於所有提到的DLL你可以檢查上面的代碼塊。 – 2015-02-06 12:20:40

1

生成MDX查詢(僅RANET OLAP 3.7版)的一個簡單的例子:

using System.Collections.Generic; 
using Ranet.Olap.Core.Data; 
using Ranet.Olap.Core.Managers; 
using Ranet.Olap.Core.Types; 
using Ranet.Olap.Core.Wrappers; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      startWork(); 
     } 

     public static void startWork() 
     { 

      var mdx = new QueryBuilderParameters 
      { 
       CubeName = "[Adventure Works]", 
       SubCube = "", 
       MdxDesignerSetting = new MDXDesignerSettingWrapper(), 
       CalculatedMembers = new List<CalcMemberInfo>(), 
       CalculatedNamedSets = new List<CalculatedNamedSetInfo>(), 
       AreaWrappersFilter = new List<AreaItemWrapper>(), 
       AreaWrappersColumns = new List<AreaItemWrapper>(), 
       AreaWrappersRows = new List<AreaItemWrapper>(), 
       AreaWrappersData = new List<AreaItemWrapper>() 
      }; 

      //define parameters 
      mdx.MdxDesignerSetting.HideEmptyColumns = false; 
      mdx.MdxDesignerSetting.HideEmptyRows = false; 
      mdx.MdxDesignerSetting.UseVisualTotals = false; 
      mdx.MdxDesignerSetting.SubsetCount = 0; 

      var itemCol1 = new Hierarchy_AreaItemWrapper 
      { 
       AreaItemType = AreaItemWrapperType.Hierarchy_AreaItemWrapper, 
       UniqueName = "[Customer].[Customer Geography]" 
      }; 
      mdx.AreaWrappersColumns.Add(itemCol1); 

      var itemRow1 = new Hierarchy_AreaItemWrapper 
      { 
       AreaItemType = AreaItemWrapperType.Hierarchy_AreaItemWrapper, 
       UniqueName = "[Date].[Calendar]" 
      }; 
      mdx.AreaWrappersRows.Add(itemRow1); 

      var itemData1 = new Measure_AreaItemWrapper(); 
      itemData1.AreaItemType = AreaItemWrapperType.Measure_AreaItemWrapper; 
      itemData1.UniqueName = "[Measures].[Internet Order Count]"; 
      mdx.AreaWrappersData.Add(itemData1); 

      string query = MdxQueryBuilder.Default.BuildQuery(mdx, null); 

     } 
    } 
} 

MDX查詢結果:

SELECT 
HIERARCHIZE(HIERARCHIZE([Customer].[Customer Geography].Levels(0).Members)) DIMENSION PROPERTIES PARENT_UNIQUE_NAME, HIERARCHY_UNIQUE_NAME, CUSTOM_ROLLUP, UNARY_OPERATOR, KEY0 ON 0, 
HIERARCHIZE(HIERARCHIZE([Date].[Calendar].Levels(0).Members)) DIMENSION PROPERTIES PARENT_UNIQUE_NAME, HIERARCHY_UNIQUE_NAME, CUSTOM_ROLLUP, UNARY_OPERATOR, KEY0 ON 1 
FROM 
[Adventure Works] 
WHERE ([Measures].[Internet Order Count]) 
CELL PROPERTIES BACK_COLOR, CELL_ORDINAL, FORE_COLOR, FONT_NAME, FONT_SIZE, FONT_FLAGS, FORMAT_STRING, VALUE, FORMATTED_VALUE, UPDATEABLE, ACTION_TYPE 
+0

下載此版本[Mdx Query builder示例]的Ranet.Olap和解決方案(https://ranetuilibraryolap.codeplex.com/releases/view/621133) – 2016-10-20 15:34:46