2013-04-09 141 views
0

我有一個多維數據集,我試圖使用以下代碼檢索數據。我不知道列和行的查詢數將會返回。 我只想讀取每一列上每列的值。使用ADOMD從SSAS多維數據集獲取數據XMLReader

void OutputDataWithXML() 
     { 
      //Open a connection to the local server. 
      AdomdConnection conn = new AdomdConnection("Data Source=localhost"); 
      conn.Open(); 

      //Create a command to retrieve the data. 
      AdomdCommand cmd = new AdomdCommand(@"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]", conn); 

      //Execute the command, retrieving an XmlReader. 
      System.Xml.XmlReader reader = cmd.ExecuteXmlReader(); 

      **// How to get the values form each column here ???? 
    // I just want to read value of each column going over each row** 
      Console.WriteLine(reader.ReadOuterXml()); 

      //Close the reader, then the connection 
      reader.Close(); 
      conn.Close(); 

      //Await user input. 
      Console.ReadLine(); 
     } 

以下鏈接說,從SSAS多維數據集檢索數據的最快方法是XMLReader的

http://msdn.microsoft.com/en-us/library/ms123479(v=sql.105).aspx

+0

「處於斷開狀態檢索數據」您複製此[MSDN文章]這個代碼(http://msdn.microsoft.com/en-us /library/ms123462.aspx)。你有沒有設置數據庫? 'reader.ReadOuterXml()'返回什麼? – 2013-04-09 03:43:08

+0

我想知道如何檢索數據,在這種情況下,你不知道有多少列查詢將返回。 – SharpCoder 2014-06-25 08:31:03

+0

是的,**那麼reader.ReadOuterXml()返回什麼?** – 2014-06-26 00:48:48

回答

0

一旦你有你的XmlReader,你可能想要得到一個單元集讀出數據和元數據。

看到部分https://technet.microsoft.com/en-us/library/ms123476(v=sql.110).aspx

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 
} 
相關問題