2012-04-02 56 views
2

我用下面的代碼來執行在C#中的查詢:查詢OLAP服務器

AdomdConnection con = new AdomdConnection("Datasource=local;..."); 

      con.Open(); 
      AdomdCommand command = con.CreateCommand(); 
      command.CommandText = input; 

      AdomdDataReader reader = command.ExecuteReader(); 
    while (reader.Read()) 
      { 
for(i =0; i<reader.fieldCount; i++){ 
     a[i]=reader.GetString(i); 
} 
return a; 

Howeever,該代碼在每個單元的層次結構返回的完整路徑。即,每行數據都像[AllGeography,Canada,Vancouver,Allproduct,bike,accessories,297483]。 我想只檢索葉子和度量值:[vancouver,accessories,297483]。我該怎麼辦?我如何指定葉子?

回答

2

因爲MDX查詢的結果實際上是多維的,所以我覺得自己更熟悉ExecuteCellSet。您可以獲得整個CellSet,然後通過座標獲取度量值。

例如(如果你有一個查詢衡量):

AdomdCommand cmd = conn.CreateCommand(); 
cmd.CommandText = @"SELECT 
      [Geography].[Geography].[Country].&[Canada].Children ON 0, 
      [Product].[Id] ON 1 
      FROM [Cube] 
      WHERE [Measures].[Your Measure]"; 

CellSet cs = cmd.ExecuteCellSet(); 

TupleCollection CanadaChildren = cs.Axes[0].Set.Tuples; 
TupleCollection ProductIds = cs.Axes[1].Set.Tuples; 

for (int row = 0; row < CanadaChildren.Count; row++) 
{ 
    for (int col = 0; col < ProductIds.Count; col++) 
    { 
     a[i++] = cs.Cells[col, row].Value; 
    } 
} 
conn.Close(); 

如果你有幾個措施,比它會在查詢第三漁政船和單元集第三cooridinate。