共用方式為


使用 CellSet 擷取資料

當擷取分析資料時,CellSet 物件可提供最大的互動性和彈性。 CellSet 物件是階層式資料和中繼資料的記憶體中快取,其中保留了資料的原始維度性。 CellSet 物件也可能以連接或中斷連接的狀態來周遊。 因為這個中斷連接的能力,CellSet 物件可用來以任何順序檢視資料和中繼資料,並為資料擷取提供最完整的物件模型。 這個中斷連接的功能也會造成 CellSet 物件有最大的負擔,並成為擴展最慢的 ADOMD.NET 資料擷取物件模型。

在連接狀態下擷取資料

若要使用 CellSet 物件擷取資料,請遵循以下步驟:

  1. 建立物件的新執行個體。

    若要建立 CellSet 物件的新執行個體,請呼叫 AdomdCommand 物件的 ExecuteExecuteCellSet 方法。

  2. 識別中繼資料。

    除了擷取資料之外,ADOMD.NET 也會為資料格集擷取中繼資料。 只要該命令已執行查詢且傳回 CellSet,您可以透過各種物件來擷取中繼資料。 當用戶端應用程式要顯示資料格集資料並與其互動時,就需要這個中繼資料。 例如,許多用戶端應用程式提供向下鑽研的功能,或是以階層方式顯示資料格集中指定位置的子系位置之功能。

    在 ADOMD.NET 中,CellSet 物件的 AxesFilterAxis 屬性,在傳回的資料格集中分別代表查詢與 Slicer 軸的中繼資料。 兩個屬性都會傳回 Axis 物件的參考,依序包含在每個軸上所代表的位置。

    每個 Axis 物件都包含代表該軸可用 Tuple 集合的 Position 物件集合。 每個 Position 物件都代表單一 Tuple,其中包含一或多個由 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 傳回。 此範例接著會使用這個 System.Xml.XmlReader 加上 LoadXml 方法來擴展 CellSet 物件。 雖然這個範例會立即載入 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
}

請參閱

參考

從分析資料來源擷取資料

使用 AdomdDataReader 擷取資料

使用 XmlReader 擷取資料