C#中應用ADOMD.NET查詢多維數據集的完成辦法。本站提示廣大學習愛好者:(C#中應用ADOMD.NET查詢多維數據集的完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中應用ADOMD.NET查詢多維數據集的完成辦法正文
本文實例講述了C#中應用ADOMD.NET查詢多維數據集的完成辦法,分享給年夜家供年夜家參考。詳細完成辦法剖析以下:
ADOMD.NET 是用於與 Microsoft SQL Server Analysis Services 停止通訊的 Microsoft .NET Framework 數據拜訪接口。 ADOMD.NET 可以使用 XML for Analysis 協定與剖析數據源通訊,辦法為應用 TCP/IP 或 HTTP 銜接傳輸和吸收相符 XML for Analysis 標准的 SOAP 要求和呼應。 敕令可經由過程多維表達式 (MDX)、數據發掘擴大插件 (DMX)、Analysis Services 劇本說話 (ASSL) 或許乃至是無限 SQL 語法來發送,而且能夠不前往成果。 可使用 ADOMD.NET 對象模子來查詢和操作剖析數據、症結績效目標 (KPI) 和發掘模子。 應用 ADOMD.NET 時,還可經由過程檢索與 OLE DB 兼容的架構行集或許應用 ADOMD.NET 對象模子來檢查和應用元數據。
ADOMD.NET 數據拜訪接口由 Microsoft.AnalysisServices.AdomdClient 定名空間表現
ADOMD.NET裝置包點擊此處下載:
實戰,銜接並查詢多維數據集:
string connectionString = "Data Source=localhost;Catalog=MDX Step-by-Step;ConnectTo=11.0;Integrated Security=SSPI";
AdomdConnection _connection = new AdomdConnection(connectionString);
if (_connection != null)
if (_connection.State == ConnectionState.Closed)
_connection.Open();
AdomdCommand command = _connection.CreateCommand();
StringBuilder sb = new StringBuilder();
sb.Append("WITH");
sb.Append(" MEMBER [Product].[Category].[All Products].[X] AS 1+1");
sb.Append("SELECT{ ([Date].[Calendar].[CY 2002]),([Date].[Calendar].[CY 2003])}*{([Measures].[Reseller Sales Amount]) } ON COLUMNS,");
sb.Append("{ ([Product].[Category].[Accessories]),([Product].[Category].[Bikes]),([Product].[Category].[Clothing]),");
sb.Append("([Product].[Category].[Components]),([Product].[Category].[X])} ON ROWS");
sb.Append(" FROM [Step-by-Step]");
command.CommandText = sb.ToString();
var xmlreader = command.ExecuteXmlReader();
CellSet cellSet = CellSet.LoadXml(xmlreader);
_connection.Close();
var dt = ToDataTable(cellSet);
var v = dt.Rows.Count;
AdomdHelper.cs文件以下:
using System;
using System.Data;
using Microsoft.AnalysisServices.AdomdClient;
namespace WpfApplication1
{
/// <summary>
/// Summary description for AdomdHelper.
/// </summary>
public class AdomdHelper
{
#region "== Enum ============================================================"
public enum Versions
{
Server,
Provider,
Client
}
#endregion
#region "== Methods ============================================================"
//斷定銜接AdomdConnection對象是State能否處於Open狀況。
public bool IsConnected(ref AdomdConnection connection)
{
return (!(connection == null)) && (connection.State != ConnectionState.Broken) && (connection.State != ConnectionState.Closed);
}
/// <summary>
/// 斷開銜接
/// </summary>
/// <param name="connection">AdomdConnection對象的實例</param>
/// <param name="destroyConnection">能否燒毀銜接</param>
public void Disconnect(ref AdomdConnection connection, bool destroyConnection)
{
try
{
if (!(connection == null))
{
if (connection.State != ConnectionState.Closed)
{
connection.Close();
}
if (destroyConnection == true)
{
connection.Dispose();
connection = null;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 樹立銜接
/// </summary>
/// <param name="connection">AdomdConnection對象的實例</param>
/// <param name="connectionString">銜接字符串</param>
public void Connect(ref AdomdConnection connection, string connectionString)
{
if (connectionString == "")
throw new ArgumentNullException("connectionString", "The connection string is not valid.");
// Ensure an AdomdConnection object exists and that its ConnectionString property is set.
if (connection == null)
connection = new AdomdConnection(connectionString);
else
{
Disconnect(ref connection, false);
connection.ConnectionString = connectionString;
}
try
{
connection.Open();
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 獲得OLAP數據庫。
/// </summary>
/// <param name="connection">AdomdConnection對象的實例</param>
/// <param name="connectionString">銜接字符串</param>
/// <returns></returns>
public DataTable GetSchemaDataSet_Catalogs(ref AdomdConnection connection, string connectionString)
{
bool connected = true; //斷定connection在挪用此函數時,能否曾經處於銜接狀況
DataTable objTable = new DataTable();
try
{
// Check if a valid connection was provided.
if (IsConnected(ref connection) == false)
{
//假如銜接不存在,則樹立銜接
Connect(ref connection, connectionString);
connected = false; //更改connection為未銜接狀況。
}
objTable = connection.GetSchemaDataSet(AdomdSchemaGuid.Catalogs, null).Tables[0];
if (connected == false)
{
//封閉銜接
Disconnect(ref connection, false);
}
}
catch (Exception err)
{
throw err;
}
return objTable;
}
/// <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(ref connection) == false)
{
try
{
Connect(ref connection, connectionString);
connected = false;
}
catch (Exception err)
{
throw err;
}
}
string[] strRestriction = new string[] { null, null, null };
objTable = connection.GetSchemaDataSet(AdomdSchemaGuid.Cubes, strRestriction).Tables[0];
if (connected == false)
{
Disconnect(ref connection, 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;
}
/// <summary>
/// 經由過程SchemaDataSet的方法獲得制訂立方體的維度
/// </summary>
/// <param name="connection">AdomdConnection對象的實例</param>
/// <param name="connectionString">銜接字符串</param>
/// <returns></returns>
public string[] GetSchemaDataSet_Dimensions(ref AdomdConnection connection, string connectionString, string cubeName)
{
string[] strDimensions = null;
bool connected = true; //斷定connection能否已與數據庫銜接
DataTable objTable = new DataTable();
if (IsConnected(ref connection) == false)
{
try
{
Connect(ref connection, connectionString);
connected = false;
}
catch (Exception err)
{
throw err;
}
}
string[] strRestriction = new string[] { null, null, cubeName, null, null };
objTable = connection.GetSchemaDataSet(AdomdSchemaGuid.Dimensions, strRestriction).Tables[0];
if (connected == false)
{
Disconnect(ref connection, 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;
}
/// <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;
bool connected = true; //斷定connection能否已與數據庫銜接
if (IsConnected(ref connection) == false)
{
try
{
Connect(ref connection, connection.ConnectionString);
connected = false;
}
catch (Exception err)
{
throw err;
}
}
int rowcount = connection.Cubes.Count;
strCubesName = new string[rowcount];
for (int i = 0; i < rowcount; i++)
{
strCubesName[i] = connection.Cubes[i].Caption;
}
if (connected == false)
{
Disconnect(ref connection, false);
}
return strCubesName;
}
/// <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;
bool connected = true;
if (IsConnected(ref connection) == false)
{
try
{
Connect(ref connection, connection.ConnectionString);
connected = false;
}
catch (Exception err)
{
throw err;
}
}
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();
}
if (connected == false)
{
Disconnect(ref connection, false);
}
return strDimensions;
}
#endregion
}
}
完全實例代碼點擊此處本站下載。
願望本文所述對年夜家的C#法式設計有所贊助。