다음을 통해 공유


CellSet을 사용하여 데이터 검색

분석 데이터를 검색할 때 개체는 CellSet 가장 상호 작용성과 유연성을 제공합니다. 개체는 CellSet 데이터의 원래 차원을 유지하는 계층적 데이터 및 메타데이터의 메모리 내 캐시입니다. 개체를 CellSet 연결된 상태 또는 연결이 끊긴 상태로 트래버스할 수도 있습니다. 이 연결이 끊어진 기능으로 인해 개체를 CellSet 사용하여 모든 순서대로 데이터 및 메타데이터를 볼 수 있으며 데이터 검색을 위한 가장 포괄적인 개체 모델을 제공합니다. 또한 이 연결이 끊어진 접근 권한 값으로 인해 CellSet 개체의 오버헤드가 가장 많고 데이터 검색 개체 모델이 채워지는 ADOMD.NET 가장 느립니다.

연결된 상태에서 데이터 검색

개체를 CellSet 사용하여 데이터를 검색하려면 다음 단계를 수행합니다.

  1. 개체의 새 인스턴스를 만듭니다.

    개체의 새 인스턴스를 CellSet 만들려면 개체의 Execute 메서드 또는 ExecuteCellSet 메서드를 호출합니다 AdomdCommand .

  2. 메타데이터를 식별합니다.

    ADOMD.NET에서는 데이터뿐 아니라 셀 집합의 메타데이터도 검색합니다. 명령이 쿼리를 실행하고 반환 CellSet하는 즉시 다양한 개체를 통해 메타데이터를 검색할 수 있습니다. 이 메타데이터는 클라이언트 응용 프로그램에서 셀 집합 데이터를 표시하고 상호 작용을 수행하는 데 필요합니다. 예를 들어, 많은 클라이언트 응용 프로그램에서 셀 집합에 있는 지정된 위치의 자식 위치를 드릴다운하거나 계층적으로 표시하는 기능을 제공합니다.

    ADOMD.NET AxesFilterAxis 개체 및 CellSet 속성은 반환된 셀 집합에서 쿼리 및 슬라이서 축의 메타데이터를 각각 나타냅니다. 두 속성 모두 개체에 대한 참조를 Axis 반환하며, 각 축에 표시되는 위치가 포함됩니다.

    Axis 개체에는 해당 축에 사용할 수 있는 튜플 집합을 나타내는 개체 컬렉션 Position 이 포함되어 있습니다. 각 Position 개체는 개체 컬렉션 Member 으로 표현되는 하나 이상의 멤버를 포함하는 단일 튜플을 나타냅니다.

  3. 셀 집합 컬렉션에서 데이터 검색

    ADOMD.NET에서는 메타데이터뿐 아니라 셀 집합의 데이터도 검색합니다. 명령이 쿼리를 실행하고 반환CellSet되는 즉시 , 의 CellSet컬렉션을 사용하여 Cells 데이터를 검색할 수 있습니다. 이 컬렉션에는 쿼리의 모든 축이 교차되는 지점을 계산한 값이 들어 있습니다. 따라서 각 교차하는 지점이나 셀에 액세스하기 위해 여러 인덱서가 존재합니다. 인덱서 목록은 을 참조하세요 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 사용하여 개체를 채우는 CellSet 예제입니다 . 메서드를 사용하는 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
}