简体   繁体   中英

How to retrieve data from mdx query in c#?

I am trying to get data from an MDX query using the Adomdclient library. I relied on this example http://www.yaldex.com/sql_server/progsqlsvr-CHP-20-SECT-6.html .

MDX query:

SELECT {[Measures].[Cantidad Vta],[Measures].[Monto Vta],[Measures].[ExistenciaHistorica],[Measures].[Valor Inventario historico]} DIMENSION PROPERTIES PARENT_UNIQUE_NAME ON COLUMNS , NON EMPTY Hierarchize({DrilldownLevel({[DIM SUBMARCA].[Código].[All]})}) DIMENSION PROPERTIES PARENT_UNIQUE_NAME ON ROWS FROM (SELECT ({[DIM TIENDA].[JERARQUIA TIENDA].[Región].&[Bodega],[DIM TIENDA].[JERARQUIA TIENDA].[Región].&[Cadena],[DIM TIENDA].[JERARQUIA TIENDA].[Región].&[Outlet]}) ON COLUMNS FROM [JUGUETRONHQ]) WHERE ([DIM FECHA VENTA].[JERARQUIA FECHA VENTA].[Time].&[2012-01-01T00:00:00],[DIM FECHA EXISTENCIA].[JERARQUIA FECHA EXISTENCIA].[All]) CELL PROPERTIES VALUE

Like other namespaces such as SqlClient, use a connection, a command and a datareader:

using Microsoft.AnalysisServices.AdomdClient;

...

using (AdomdConnection con = new AdomdConnection(connection_string))
{
  con.Open();

  using (AdomdCommand command = new AdomdCommand(query, con)) 
  {
    using (AdomdDataReader reader = command.ExecuteReader()) 
    {
      while (reader.Read())
      {
        for (int i = 0; i < reader.FieldCount; i++)
          Console.Write(reader[i] + (i == reader.FieldCount - 1 ? "" : ", "));
          Console.WriteLine("");

      }
    }
  }
}

However, this snippet only shows 4 of 5 columns correctly:

[DIM SUBMARCA].[Código].[All], , , 3, 825

It must be:

115200081, , , 3, 825

Perhaps need a cast but I don't know how to do it.

This looks like a problem with the MDX query, not the retrieval of the data. It's not correctly constraining on the [DIM SUBMARCA].[Código] dimension.

Retrieve data from MDX query

added the reference for Microsoft.AnalysisServices.AdomdClient.dll
AdomdConnection steps

AdomdConnection con = new AdomdConnection("connectionstring"); // connect DB con.Open(); AdomdCommand cmd = new AdomdCommand("MDX query", con); //query

AdomdDataReader reader = cmd.ExecuteReader(); //Execute query

    while (reader.Read())   // read
        {
            Data dt = new Data();  // custom class
            dt.Gender = reader[0].ToString();

            dt.Eid = reader[1].ToString();
            dt.salary = reader[2].ToString();
            data.Add(dt);
        }

Your query has 1 [ALL] Level Dimension and 4 measures:

[DIM SUBMARCA].[Código].[All], 
[Measures].[Cantidad Vta],
[Measures].[Monto Vta],
[Measures].[ExistenciaHistorica],
[Measures].[Valor Inventario historico]

This retrieves 1 [ALL] column and 4 Values:

[DIM SUBMARCA].[Código].[All], , , 3, 825

115200081 is a key value? You could get this value using "DIMENSION PROPERTIES MEMBER_HEY".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM