關於如何遠程訪問AnalysisServices以及Cell向DataTable轉換的關鍵技術

下邊簡單的說一下我前段時間整理的從Analysis Services讀取數據,然後轉換爲DataTable的解決方法及思路。

從以下幾個步驟說明:

:配置AnalysisServices的遠程http訪問:http://blogs.msdn.com/b/sqldev/archive/2011/01/18/how-to-configure-http-access-to-sql-server-2008-analysis-services-on-iis7.aspx

配置完成以後就可以在程序中訪問AnalysisServices服務了,下邊是應用程序端的配置。

: 在項目引用中引用Microsoft.AnalysisServices.AdomdClient 文件位置在

C:\Program Files(x86)\Microsoft.NET\ADOMD.NET\100\Microsoft.AnalysisServices.AdomdClient.dll 沒有的話需要去下載,一般都有。

:配置AnalysisServices訪問鏈接:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Collections;
namespaceBeisen.AnalysisServices.ServiceImp.Config
{
   public sealed class AnalysisServicesConfig
    {
      public static readonly stringAnalysisServicesHttpConn4 = "Provider=MSOLAP;
      DataSource=http://localhost/olap/msmdpump.dll;
       Initial Catalog=BeiSenAnalysisServices;";
    
    }
}


四:下邊就可以訪問數據了:

protected DataTableGetData()
       {
           string SQLQuery = @"select[Wd Highest Degree].[Name].[Name] on columns,([Wd Datetime].[DYear].children,
          [Wd Datetime].[Dmonth].children,[Wd Datetime].[Ddate].children)on rows from [Beisen ASA];";
           if (DataTable != null)return DataTable;
           if (DBConnectionString == null || SQLQuery == null)
           throw new Exception("Pleaseprovide either DataRows, 
           or both DBConnectionString and SQLQuery to getdata");
           AdomdConnection conn = new AdomdConnection(DBConnectionString);
           AdomdCommand comm = newAdomdCommand(SQLQuery, conn);
           conn.Open();
           CellSet cs = comm.ExecuteCellSet();
           conn.Close();
           return CellSetToDataTable.ToDataTable(cs,"table0");
       }


五:到這就剩下需要找到ToDataTable方法了,下邊是這個方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
usingMicrosoft.AnalysisServices.AdomdClient;
 
namespace  AnalysisServices.Utility
{
   public class CellSetToDataTable
    {
       /// <summary>
       /// 將CellSet轉化成DataTable(包括所有維度)
       /// </summary>
       /// <param name="cs">CellSet</param>
       /// <returns></returns>
       public static DataTable ToDataTable(CellSetcs,string tbname)
       {
           DataTable dt = newDataTable(tbname);
           DataColumn dc = null;
           DataRow dr = null;
 
            //生成數據列對象
           //多個維度轉化成列          
 for (int col = 0; col< cs.Axes[1].Set.Hierarchies.Count; col++)
           {
               dc = new DataColumn();
//下面的代碼會報錯:"The connection is not open.” 獲取層次結構的維度名時需要連接Cube纔可以!
//dt.Columns.Add(new DataColumn(cs.Axes[1].Set.Hierarchies[col].ParentDimension.Name));
               dt.Columns.Add(new DataColumn("Dimension" + col.ToString()));
           }
 
           int index = 0;
           foreach (Positionp in cs.Axes[0].Positions)
           {
               dc = new DataColumn();
               string name = "";
               foreach (Memberm in p.Members)
               {
                   name += m.Caption + "-";
               }
               if (name.Length > 0)
               {
                   name = name.Substring(0, name.Length - 1);
               }
                //這裏防止維度成員或度量值重名而需要容錯處理
               try
               {
                   dc.ColumnName = name;
                   dt.Columns.Add(dc);
               }
               catch (System.Exceptionex)
               {
                   dc.ColumnName = name + index.ToString();
                   dt.Columns.Add(dc);
               }
               index++;
           }
 
           //添加行數據
           int pos = 0;
           foreach (Positionpy in cs.Axes[1].Positions)
           {
               dr = dt.NewRow();
 
               //維度描述列數據
               intcols = 0;
               foreach (Memberm in py.Members)
               {
                   dr[cols] = m.Caption;
                   cols++;
               }
 
               //數據列
               for (int x = 1;x <= cs.Axes[0].Positions.Count; x++)
               {
                   dr[x + cols - 1] = cs[pos++].FormattedValue;
               }
               dt.Rows.Add(dr);
           }
           return dt;
       }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章