使用 CellSet 擷取資料

在抓取分析資料時, CellSet 物件會提供最高的互動性和彈性。 CellSet物件是階層式資料和中繼資料的記憶體中快取,可保留資料的原始維度。 CellSet物件也可以在已連線或已中斷連線的狀態中進行。 由於這個中斷連接的能力, CellSet 物件可以用任何順序來查看資料和中繼資料,並為數據抓取提供最全面的物件模型。 這個中斷連接的 CellSet 功能也會造成物件的額外負荷,而且是最慢的 ADOMD.NET 資料抓取物件模型。

在連接狀態下擷取資料

若要使用 CellSet 物件來取出資料,請遵循下列步驟:

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

    若要建立物件的新實例 CellSet ,請呼叫物件的 ExecuteExecuteCellSet 方法 AdomdCommand

  2. 識別中繼資料。

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

    在 ADOMD.NET 中,物件的 AxesFilterAxis 屬性 CellSet 分別代表傳回的資料格集內的查詢和交叉分析篩選器軸的中繼資料。 這兩個屬性都會傳回物件的參考 Axis ,而這些物件又包含每個軸上所代表的位置。

    每個 Axis 物件都包含物件的集合 Position ,這些物件表示可用於該軸的元組集。 每個物件都 Position 代表一個包含一或多個成員的單一元組,以物件的集合 Member 表示。

  3. 從資料格集集合擷取資料。

    除了擷取中繼資料之外,ADOMD.NET 也會為資料格集擷取資料。 一旦命令執行查詢並傳回 CellSet ,您就可以使用 Cells 的集合 CellSet 來取得資料。 這個集合包含為查詢中所有軸的交集所計算的值。 因此,有幾個存取每個交集或是資料格的索引子。 如需索引子的清單,請參閱 Item[]

在連接狀態下擷取資料的範例

下列範例會建立本機伺服器的連接,然後在該連接上執行命令。 此範例會使用資料 格集 物件模型來剖析結果:系統會從第一個座標軸抓取資料行 (中繼資料) 的標題,並且會從第二個軸抓取每個資料列 (中繼資料) 的標題,然後使用 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 來 CellSet 擴展物件 使用 LoadXml 方法的 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
}