一、安裝Microsoft AnalysisServies數據庫
二、建立一個Analysis 項目以下圖所示dom
三、根據業務需求,數據源及多維度數據集,編譯成功,進行佈署。ide
四、發佈成功後,經過SQLSERVER企業管理器進行鏈接,如何在IIS7下建立Analysis Services服務能夠baidu一下具體配置,這裏不在多說了。測試
五、建立一個客戶端測試ui
建立一個AdomdHelper類spa
#region using System; using System.Data; using Microsoft.AnalysisServices.AdomdClient; using System.Collections; using System.Collections.Generic; #endregion namespace WMS.Utility { /// <summary> /// Analysis Services /// </summary> public class AdomdHelper { #region Enum public enum Versions { Server, Provider, Client } #endregion #region 屬性 /// <summary> /// 多維數據庫鏈接對象 /// </summary> public AdomdConnection AdomdConn { get; set; } /// <summary> /// 鏈接字符串 /// </summary> public string ConnString { get; set; } /// <summary> /// 鏈接狀態 /// </summary> public bool IsConnected { get { return GetConnectedState(); } } #endregion #region AdomdHelper public AdomdHelper() { ConnString = 「http://localhost/olap/msmdpump.dll」; } #endregion #region GetConnectedState /// <summary> /// 判斷鏈接AdomdConnection對象是State是否處於Open狀態。 /// </summary> /// <returns></returns> public bool GetConnectedState() { bool isConn = false; isConn = (!(AdomdConn == null)) && (AdomdConn.State != ConnectionState.Broken) && (AdomdConn.State != ConnectionState.Closed); return isConn; } #endregion #region Disconnect /// <summary> /// 斷開鏈接 /// </summary> /// <param name="connection">AdomdConnection對象的實例</param> /// <param name="destroyConnection">是否銷燬鏈接</param> public void Disconnect(bool destroyConnection) { try { if (!(AdomdConn == null)) { if (AdomdConn.State != ConnectionState.Closed) { AdomdConn.Close(); } if(destroyConnection == true) { AdomdConn.Dispose(); AdomdConn = null; } } } catch(Exception ex) { throw ex; } } /// <summary> /// 斷開鏈接 /// </summary> public void Disconnect() { Disconnect(true); } #endregion #region Connect /// <summary> /// 創建鏈接 /// </summary> /// <param name="connection">AdomdConnection對象的實例</param> /// <param name="connectionString">鏈接字符串</param> public AdomdConnection Connect() { try { AdomdConn = new AdomdConnection(ConnString); AdomdConn.Open(); }catch (Exception ex) { string str = ex.Message; } return AdomdConn; } /// <summary> /// 獲取OLAP數據庫。 /// </summary> /// <param name="connection">AdomdConnection對象的實例</param> /// <param name="connectionString">鏈接字符串</param> /// <returns></returns> public DataTable GetSchemaDataSet_Catalogs() { DataTable objTable = new DataTable(); try { if(!IsConnected)Connect(); objTable = AdomdConn.GetSchemaDataSet(AdomdSchemaGuid.Catalogs,null).Tables[0]; Disconnect(false); } catch(Exception err) { throw err; } return objTable; } #endregion #region GetSchemaDataSet_Cubes /// <summary> /// 經過SchemaDataSet的方式獲取立方體 /// </summary> /// <param name="connection">AdomdConnection對象的實例</param> /// <param name="connectionString">鏈接字符串</param> /// <returns></returns> public string[] GetSchemaDataSet_Cubes(ref AdomdConnection connection,string connectionString) { string[] strCubes = null; bool connected = true; //判斷connection是否已與數據庫鏈接 DataTable objTable = new DataTable(); if (!IsConnected) Connect(); string[] strRestriction = new string[] { null, null, null }; objTable = connection.GetSchemaDataSet(AdomdSchemaGuid.Cubes,strRestriction).Tables[0]; if(connected == false) { Disconnect(false); } strCubes = new string[objTable.Rows.Count]; int rowcount = 0; foreach(DataRow tempRow in objTable.Rows) { strCubes[rowcount] = tempRow["CUBE_NAME"].ToString(); rowcount++; } return strCubes; } #endregion #region GetSchemaDataSet_Dimensions /// <summary> /// 經過SchemaDataSet的方式獲取制定立方體的維度 /// </summary> /// <param name="connection">AdomdConnection對象的實例</param> /// <param name="connectionString">鏈接字符串</param> /// <returns></returns> public string[] GetSchemaDataSet_Dimensions(string cubeName) { string[] strDimensions = null; DataTable objTable = new DataTable(); if (!IsConnected) Connect(); string[] strRestriction = new string[]{null,null,cubeName,null,null}; objTable = AdomdConn.GetSchemaDataSet(AdomdSchemaGuid.Dimensions,strRestriction).Tables[0]; Disconnect(false); strDimensions = new string[objTable.Rows.Count]; int rowcount = 0; foreach(DataRow tempRow in objTable.Rows) { strDimensions[rowcount] = tempRow["DIMENSION_NAME"].ToString(); rowcount++; } return strDimensions; } #endregion #region GetCubes /// <summary> /// 以connection的方式立方體 /// </summary> /// <param name="connection">AdomdConnection對象的實例</param> /// <param name="connectionString">鏈接字符串</param> /// <returns></returns> public string[] GetCubes(ref AdomdConnection connection,string connectionString) { string[] strCubesName = null; if (!IsConnected) Connect(); int rowcount = connection.Cubes.Count; strCubesName = new string[rowcount]; for(int i=0;i<rowcount;i++) { strCubesName[i] = connection.Cubes[i].Caption; } Disconnect(false); return strCubesName; } #endregion #region GetDimensions /// <summary> /// 獲取維度 /// </summary> /// <param name="connection">AdomdConnection對象的實例</param> /// <param name="connectionString">鏈接字符串</param> /// <param name="CubeName">名稱</param> /// <returns></returns> public string[] GetDimensions(ref AdomdConnection connection,string connectionString,string CubeName) { string[] strDimensions = null; if (!IsConnected) Connect(); int rowcount = connection.Cubes[CubeName].Dimensions.Count; strDimensions = new string[rowcount]; for(int i=0;i<rowcount;i++) { strDimensions[i] = connection.Cubes[CubeName].Dimensions[i].Caption.ToString(); } Disconnect(false); return strDimensions; } #endregion #region GetCellSet /// <summary> /// 根據mdx語句返回CellSet /// </summary> /// <param name="strMDX">MDX語句</param> /// <returns></returns> public CellSet GetCellSet(string strMDX) { if (!IsConnected) Connect(); AdomdCommand comm = new AdomdCommand(strMDX, AdomdConn); CellSet cs = comm.ExecuteCellSet(); return cs; } #endregion #region GetCellSet2DataTable /// <summary> /// 根據mdx語句獲取CellSet,同時轉換爲DataTable /// </summary> /// <param name="strMDX">MDX語句</param> /// <returns></returns> public DataTable GetCellSet2DataTable(string strMDX) { if (!IsConnected) Connect(); AdomdCommand comm = new AdomdCommand(strMDX, AdomdConn); CellSet cs = comm.ExecuteCellSet(); DataTable dt = CellSetToTable(cs); return dt; } #endregion #region CellSetToTable /// <summary> /// 將CellSet轉化成Table /// </summary> /// <param name="cellset">CellSet</param> /// <returns></returns> public DataTable CellSetToTable(CellSet cellset) { DataTable table = new DataTable("cellset"); int columnIndex = 0; Axis columns = cellset.Axes[0]; //獲取列軸 Axis rows = cellset.Axes.Count > 1 ? cellset.Axes[1] : null;//獲取行軸 CellCollection valuesCell = cellset.Cells;//獲取度量值單元集合 //行軸的級別標題爲表的列 for (int i = 0; rows != null && i < rows.Set.Hierarchies.Count; i++) { DataColumn col = new DataColumn(); col.ColumnName = "col_" + i; col.Caption = rows.Set.Hierarchies[i].Caption; table.Columns.Add(col); columnIndex++; } //行軸的各個成員的標題變成表的列 for (int i = 0; i < columns.Set.Tuples.Count; i++) { MemberCollection mc = columns.Set.Tuples[i].Members; DataColumn col = new DataColumn(); col.ColumnName = "col_" + columnIndex; col.Caption = mc[mc.Count - 1].Caption; table.Columns.Add(col); columnIndex++; } int valueIndex = 0; DataRow row = null; if (rows != null) { //向表中填充數據 for (int i = 0; i < rows.Set.Tuples.Count; i++) { row = table.NewRow(); //表全部行的層級的標題 MemberCollection mc = rows.Set.Tuples[i].Members; int j = 0; for (j = 0; j < mc.Count; j++) { row[j] = mc[j].Caption.Replace("All", "合計"); } for (int k = 0; k < columns.Set.Tuples.Count; k++) {//按順序把度量值單元集合的值填充到表中 row[k + j] = valuesCell[valueIndex].Value; valueIndex++; } table.Rows.Add(row); } } else { //只有一個維度時 row = table.NewRow(); for (int i = 0; i < columns.Set.Tuples.Count; i++) { row[i] = valuesCell[valueIndex].Value; valueIndex++; } table.Rows.Add(row); } return table; } #endregion #region GetMeasureGroup /// <summary> /// 得到度量值組 /// </summary> /// <param name="cubeName">名稱</param> /// <returns></returns> public IList<Dictionary<int, string>> GetMeasureGroup(string cubeName) { if (!IsConnected) Connect(); IList<Dictionary<int, string>> groupList = new List<Dictionary<int, string>>(); string mdx = "SELECT * FROM $SYSTEM.MDSCHEMA_MEASUREGROUPS Where CUBE_NAME = '" + cubeName + "';"; AdomdCommand comm = new AdomdCommand(mdx, AdomdConn); AdomdDataReader reader = comm.ExecuteReader(); int i=0; while (reader.Read()) { string name = reader["MEASUREGROUP_NAME"].ToString(); Dictionary<int, string> row = new Dictionary<int, string>(); row.Add(i, name); groupList.Add(row); i++; } reader.Close(); return groupList; } #endregion #region GetMeasures /// <summary> /// 得到度量值 /// </summary> /// <param name="cubeName">名稱</param> /// <returns></returns> public IList<Dictionary<int, string>> GetMeasures(string cubeName, string measureGroupName) { if (!IsConnected) Connect(); IList<Dictionary<int, string>> groupList = new List<Dictionary<int, string>>(); string mdx = "SELECT * FROM $SYSTEM.MDSCHEMA_MEASURES Where CUBE_NAME = '" + cubeName + "' And MEASUREGROUP_NAME='" + measureGroupName + "';"; AdomdCommand comm = new AdomdCommand(mdx, AdomdConn); AdomdDataReader reader = comm.ExecuteReader(); int i = 0; while (reader.Read()) { string name = reader["MEASURE_NAME"].ToString(); Dictionary<int, string> row = new Dictionary<int, string>(); row.Add(i, name); groupList.Add(row); i++; } reader.Close(); return groupList; } #endregion #region GetDimensions /// <summary> /// 得到維度 /// </summary> /// <param name="cubeName">名稱</param> /// <returns></returns> public IList<Dictionary<int, string>> GetDimensions(string cubeName) { if (!IsConnected) Connect(); IList<Dictionary<int, string>> groupList = new List<Dictionary<int, string>>(); string mdx = "SELECT * FROM $SYSTEM.MDSCHEMA_DIMENSIONS Where CUBE_NAME = '" + cubeName + "' And DIMENSION_TYPE<>2;"; AdomdCommand comm = new AdomdCommand(mdx, AdomdConn); AdomdDataReader reader = comm.ExecuteReader(); int i = 0; while (reader.Read()) { string name = reader["DIMENSION_NAME"].ToString(); Dictionary<int, string> row = new Dictionary<int, string>(); row.Add(i, name); groupList.Add(row); i++; } reader.Close(); return groupList; } #endregion #region GetHierarchys /// <summary> /// 得到維度層級結構 /// </summary> /// <param name="cubeName">名稱</param> /// <returns></returns> public IList<KeyCodeNameModel> GetHierarchys(string cubeName, string dimensionName) { if (!IsConnected) Connect(); IList<KeyCodeNameModel> groupList = new List<KeyCodeNameModel>(); string mdx = "SELECT [HIERARCHY_UNIQUE_NAME], HIERARCHY_CAPTION FROM $SYSTEM.MDSCHEMA_HIERARCHIES Where HIERARCHY_IS_VISIBLE And CUBE_NAME = '" + cubeName + "' And [DIMENSION_UNIQUE_NAME]='[" + dimensionName + "]';"; AdomdCommand comm = new AdomdCommand(mdx, AdomdConn); AdomdDataReader reader = comm.ExecuteReader(); int i = 0; while (reader.Read()) { string name = reader["HIERARCHY_UNIQUE_NAME"].ToString(); string caption = reader["HIERARCHY_CAPTION"].ToString(); KeyCodeNameModel row = new KeyCodeNameModel(); row.ID = i; row.Code = name; row.Name = caption; groupList.Add(row); i++; } reader.Close(); return groupList; } #endregion #region GetLevels /// <summary> /// 得到維度級別 /// </summary> /// <param name="cubeName">名稱</param> /// <returns></returns> public IList<KeyCodeNameModel> GetLevels(string cubeName, string hierarchyName) { if (!IsConnected) Connect(); IList<KeyCodeNameModel> groupList = new List<KeyCodeNameModel>(); KeyCodeNameModel memberRow = new KeyCodeNameModel(); memberRow.ID = 0; memberRow.Code = "成員"; memberRow.Name = "成員"; groupList.Add(memberRow); string mdx = @"SELECT LEVEL_CAPTION, [LEVEL_UNIQUE_NAME] FROM $SYSTEM.MDSCHEMA_LEVELS Where LEVEL_IS_VISIBLE AND LEVEL_CAPTION<>'(ALL)' AND CUBE_NAME = '" + cubeName + "' And [HIERARCHY_UNIQUE_NAME]='" + hierarchyName + "';"; AdomdCommand comm = new AdomdCommand(mdx, AdomdConn); AdomdDataReader reader = comm.ExecuteReader(); int i = 1; while (reader.Read()) { string name = reader["LEVEL_UNIQUE_NAME"].ToString(); string caption = reader["LEVEL_CAPTION"].ToString(); KeyCodeNameModel row = new KeyCodeNameModel(); row.ID = i; row.Code = name; row.Name = caption; groupList.Add(row); i++; } reader.Close(); return groupList; } #endregion #region GetMembers /// <summary> /// 得到維度級別 /// </summary> /// <param name="cubeName">名稱</param> /// <returns></returns> public IList<KeyCodeNameModel> GetMembers(string cubeName, string levelName) { if (!IsConnected) Connect(); IList<KeyCodeNameModel> groupList = new List<KeyCodeNameModel>(); string mdx = @" SELECT [MEMBER_UNIQUE_NAME], [MEMBER_CAPTION] FROM $SYSTEM.MDSCHEMA_MEMBERS Where CUBE_NAME = '" + cubeName + "' And [LEVEL_UNIQUE_NAME]='" + levelName + "';"; AdomdCommand comm = new AdomdCommand(mdx, AdomdConn); AdomdDataReader reader = comm.ExecuteReader(); int i = 1; while (reader.Read()) { string name = reader["MEMBER_UNIQUE_NAME"].ToString(); string caption = reader["MEMBER_CAPTION"].ToString(); KeyCodeNameModel row = new KeyCodeNameModel(); row.ID = i; row.Code = name; row.Name = caption; groupList.Add(row); i++; } reader.Close(); return groupList; } #endregion } }
六、須要求注意字符集rest
在 Microsoft.AnalysisServices.AdomdClient.HttpStream.GetResponseDataType()
在 Microsoft.AnalysisServices.AdomdClient.CompressedStream.GetResponseDataType()
在 Microsoft.AnalysisServices.AdomdClient.XmlaClient.EndRequest()
在 Microsoft.AnalysisServices.AdomdClient.XmlaClient.SendMessage(Boolean endReceivalIfException, Boolean readSession, Boolean readNamespaceCompatibility)
在 Microsoft.AnalysisServices.AdomdClient.XmlaClient.Discover(String requestType, String requestNamespace, ListDictionary properties, IDictionary restrictions, Boolean sendNamespacesCompatibility)
在 Microsoft.AnalysisServices.AdomdClient.XmlaClient.SupportsProperty(String propName)
在 Microsoft.AnalysisServices.AdomdClient.XmlaClient.Connect(ConnectionInfo connectionInfo, Boolean beginSession)
在 Microsoft.AnalysisServices.AdomdClient.AdomdConnection.XmlaClientProvider.Connect(Boolean toIXMLA)
在 Microsoft.AnalysisServices.AdomdClient.AdomdConnection.XmlaClientProvider.Microsoft.AnalysisServices.AdomdClient.AdomdConnection.IXmlaClientProviderEx.ConnectXmla()
在 Microsoft.AnalysisServices.AdomdClient.AdomdConnection.ConnectToXMLA(Boolean createSession, Boolean isHTTP)
在 Microsoft.AnalysisServices.AdomdClient.AdomdConnection.Open()
在 Rattan.Core.Utility.AdomdHelper.Connect() 位置 d:\work\wms\ERPSource_WMS\Rattan.Core\Utility\AdomdHelper.cs:行號 130code