Share via


セルセットを使用したデータの取得

オブジェクトは CellSet 、分析データを取得するときに、最も対話性と柔軟性を提供します。 CellSetオブジェクトは、データの元の次元を保持する階層データとメタデータのメモリ内キャッシュです。 また、 CellSet 接続された状態または切断状態のオブジェクトを走査することもできます。 このように接続されていない機能 CellSet により、オブジェクトを使用して任意の順序でデータとメタデータを表示し、データ取得のための最も包括的なオブジェクトモデルを提供できます。 また、この切断された機能によって、オブジェクトの CellSet オーバーヘッドが最も高くなり、最も低速な ADOMD.NET データ取得オブジェクトモデルが作成されます。

接続状態でのデータの取得

オブジェクトを使用して CellSet データを取得するには、次の手順を実行します。

  1. このオブジェクトの新しいインスタンスを作成します。

    オブジェクトの新しいインスタンス CellSet を作成するには、オブジェクトの AdomdCommand メソッドまたは ExecuteCellSet メソッドを呼び出し Execute ます。

  2. メタデータを確認します。

    データを取得する以外に、ADOMD.NET はセルセットのメタデータも取得します。 コマンドによってクエリが実行され、が返される CellSet とすぐに、さまざまなオブジェクトを使用してメタデータを取得できます。 このメタデータは、クライアント アプリケーションがセルセット データを表示したり、セルセット データを操作したりする際に必要となります。 たとえば、多くのクライアント アプリケーションには、セルセット内の指定した位置をドリル ダウンし、子の位置を階層的に表示するための機能が備わっています。

    ADOMD.NET では、オブジェクトの CellSet プロパティと FilterAxis プロパティは Axes 、返されたセルセット内のクエリ軸とスライサー軸のメタデータをそれぞれ表します。 どちらのプロパティも、オブジェクトへ Axis の参照を返します。このオブジェクトには、各軸で表される位置が含まれています。

    Axis オブジェクトには、その軸で使用可能な組のセットを表すオブジェクトの Position コレクションが含まれています。 各 Position オブジェクトは、オブジェクトの Member コレクションによって表される1つ以上のメンバーを含む1つのタプルを表します。

  3. セルセット コレクションからデータを取得します。

    メタデータを取得する以外に、ADOMD.NET はセルセットのデータも取得します。 コマンドによってクエリが実行され、が返される CellSet とすぐに、の CellSet コレクションを使用 Cells してデータを取得できます。 このコレクションには、クエリのすべての軸の交差部分について計算された値が含まれます。 したがって、各交差部分 (セル) にアクセスするためのインデクサーがいくつかあります。 インデクサーの一覧については、「」を参照して Item[] ください。

接続状態でのデータの取得例

次の例では、ローカル サーバーへの接続を確立し、その接続に対してコマンドを実行します。 この例では、 セルセット オブジェクトモデルを使用して結果を解析します。列のキャプション (メタデータ) は最初の軸から取得され、各行のキャプション (メタデータ) は2番目の軸から取得され、交差したデータはコレクションを使用 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 では、このSystem.Xml を使用してオブジェクトを設定し ます。XmlReader をメソッドと共に使用 LoadXml します。 この例では、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
}