使用 CellSet 检索数据

检索分析数据时,CellSet 对象将提供最大的交互功能和灵活性。CellSet 对象是分层数据和元数据的内存缓存,用于保留数据的原始维数。还可以在连接或断开连接状态中对 CellSet 对象进行遍历。由于此断开连接的功能,CellSet 对象还可用于以任何顺序查看数据和元数据,并为数据检索提供最全面的对象模型。此断开连接的功能还会导致 CellSet 对象有最大的开销,并成为要填充的最慢的 ADOMD.NET 数据检索对象模型。

在连接状态下检索数据

若要使用 CellSet 对象检索数据,请按以下步骤操作:

  1. 创建对象的新实例。

    若要创建 CellSet 对象的新实例,请调用 AdomdCommand 对象的 ExecuteExecuteCellSet 方法。

  2. 标识元数据。

    除了检索数据,ADOMD.NET 还将检索单元集的元数据。在该命令运行查询并返回 CellSet 后,即可通过各种对象检索元数据。客户端应用程序需要使用此元数据显示单元集数据并与之交互。例如,许多客户端应用程序提供对单元集中的特定位置进行细分,或分层显示特定位置的子位置的功能。

    在 ADOMD.NET 中,CellSet 对象的 AxesFilterAxis 属性分别代表所返回单元集中的查询轴和切片器轴的元数据。这两个属性都返回对 Axis 对象的引用,这些引用又包含每个轴上表示的位置。

    每个 Axis 对象包含 Position 对象的集合,这些对象表示可用于该轴的元组集。每个 Position 对象表示一个包含一个或多个成员的元组,这些成员由 Member 对象的集合表示。

  3. 从单元集集合检索数据。

    除了检索元数据,ADOMD.NET 还将检索单元集的数据。在该命令运行了查询并返回 CellSet 后,即可使用 CellSetCells 集合检索数据。此集合包含为查询中的所有轴的交点计算出的值。因此,有多个可用于访问每个交点或单元的索引器。有关索引器的列表,请参阅 Item

在连接状态下检索数据的示例

下面的示例连接到本地服务器,然后对该连接运行命令。该示例使用 CellSet 对象模型分析结果:列标题(元数据)从第一个轴检索,每行标题(元数据)从第二个轴检索,交点数据使用 Cells 集合检索。

string ReturnCommandUsingCellSet()
{
    //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 a cellset
        CellSet cs = cmd.ExecuteCellSet();

        //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();
        }
        conn.Close();

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

在断开连接状态下检索数据

通过加载从前一查询返回的 XML,可以使用 CellSet 对象提供全面的方法来浏览分析数据,而不需要活动连接。

注意注意

不是 CellSet 对象中提供的对象的所有属性在断开连接的状态下都可用。有关详细信息,请参阅 LoadXml

在断开连接状态下检索数据的示例

下面的示例与本主题中前面介绍的元数据和数据示例类似。但是,下面示例中运行的命令通过调用 ExecuteXmlReader 运行,并且结果作为 System.Xml.XmlReader 返回。然后,示例填充 CellSet 对象,方法是使用此具有 LoadXml 方法的 System.Xml.XmlReader。尽管此示例将立即加载 System.Xml.XmlReader,但是可以将读取器包含的 XML 缓存到硬盘,或通过任何方法将数据传输到其他应用程序,然后再将该数据加载到单元集。

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
}