2012-04-02 72 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。