Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying OLAP server

Tags:

c#

ado.net

olap

mdx

I use the following code to execute a query in C#:

 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, this code returns the full path in the hierarchy for each cell. I.e., each row of data is like [AllGeography, Canada, Vancouver, Allproduct, bikes, accessories, 297483]. I want to retrieve only the leaves and the measure value that is :[vancouver, accessories, 297483]. What should I do? How I can specify the leaves?

like image 896
Elaheh kamaliha Avatar asked Jun 12 '26 20:06

Elaheh kamaliha


1 Answers

Because the result of MDX query is actually multidimentional, i feel myself more comfortable with ExecuteCellSet. You can get the whole CellSet, then you get Measures via coordinates.

For example (if you have one measure in query):

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();

If you have several measures, than it will be a third dimention in query and a third cooridinate in a cellset.

like image 81
Andrey Ershov Avatar answered Jun 15 '26 11:06

Andrey Ershov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!