简体   繁体   中英

Getting data from SSAS cube using ADOMD XMLReader

I have a cube and I am trying to retrieve data using following code. I don't know the number of columns and rows query will return. I just want to read value of each column going over each row.

void OutputDataWithXML()
        {
            //Open a connection to the local server.
            AdomdConnection conn = new AdomdConnection("Data Source=localhost");
            conn.Open();

            //Create a command to retrieve the data.
            AdomdCommand cmd = new AdomdCommand(@"WITH MEMBER [Measures].[FreightCostPerOrder] AS 
[Measures].[Reseller Freight Cost]/[Measures].[Reseller Order Quantity],  
FORMAT_STRING = 'Currency'

SELECT [Geography].[Geography].[Country].&[United States].Children ON ROWS, 
[Date].[Calendar].[Calendar Year] ON COLUMNS
FROM [Adventure Works]
WHERE [Measures].[FreightCostPerOrder]", conn);

            //Execute the command, retrieving an XmlReader.
            System.Xml.XmlReader reader = cmd.ExecuteXmlReader();

            **// How to get the values form each column here ????
    // I just want to read value of each column going over each row**
            Console.WriteLine(reader.ReadOuterXml());

            //Close the reader, then the connection
            reader.Close();
            conn.Close();

            //Await user input.
            Console.ReadLine();
        }

Following link says the fastest way to retrieve data from SSAS cube is XMLReader

http://msdn.microsoft.com/en-us/library/ms123479(v=sql.105).aspx

Once you have your XmlReader, you'll probably want to get a CellSet to read out the data and metadata.

See https://technet.microsoft.com/en-us/library/ms123476(v=sql.110).aspx in the section "Retrieving Data in a Disconnected State"

string DemonstrateDisconnectedCellset() {
//Create a new string builder to store the results
System.Text.StringBuilder result = new System.Text.StringBuilder();

//Connect to the local server
using (AdomdConnection conn = new AdomdConnection("Data Source=localhost;"))
{
    conn.Open();

    //Create a command, using this connection
    AdomdCommand cmd = conn.CreateCommand();
    cmd.CommandText = @"
                  WITH MEMBER [Measures].[FreightCostPerOrder] AS 
                        [Measures].[Reseller Freight Cost]/[Measures].[Reseller Order Quantity],  
                        FORMAT_STRING = 'Currency'
                  SELECT 
                        [Geography].[Geography].[Country].&[United States].Children ON ROWS, 
                        [Date].[Calendar].[Calendar Year] ON COLUMNS
                  FROM [Adventure Works]
                  WHERE [Measures].[FreightCostPerOrder]";


    //Execute the query, returning an XmlReader
    System.Xml.XmlReader x = cmd.ExecuteXmlReader();

    //At this point, the XmlReader could be stored on disk,
    //transmitted, modified, cached, or otherwise manipulated

    //Load the CellSet with the specified XML
    CellSet cs = CellSet.LoadXml(x);

    //Now that the XmlReader has finished being read
    //we can close it and the connection, while the
    //CellSet can continue being used.
    x.Close();
    conn.Close();

    //Output the column captions from the first axis
    //Note that this procedure assumes a single member exists per column.
    result.Append("\t");
    TupleCollection tuplesOnColumns = cs.Axes[0].Set.Tuples;
    foreach (Tuple column in tuplesOnColumns)
    {
        result.Append(column.Members[0].Caption + "\t");
    }
    result.AppendLine();

    //Output the row captions from the second axis and cell data
    //Note that this procedure assumes a two-dimensional cellset
    TupleCollection tuplesOnRows = cs.Axes[1].Set.Tuples;
    for (int row = 0; row < tuplesOnRows.Count; row++)
    {
        result.Append(tuplesOnRows[row].Members[0].Caption + "\t");
        for (int col = 0; col < tuplesOnColumns.Count; col++)
        {
            result.Append(cs.Cells[col, row].FormattedValue + "\t");
        }
        result.AppendLine();
    }

    return result.ToString();
} // using connection
}

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