ADOMD.NET 是用于与 Microsoft SQL Server Analysis Services 进行通信的 Microsoft .NET Framework 数据访问接口。ADOMD.NET 可使用xmlfor 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安装包下载地址:
实战,连接并查询多维数据集:
stringconnectionString ="Data Source=localhost;Catalog=MDX Step-by-Step;ConnectTo=11.0;Integrated Security=SSPI";
AdomdConnection _connection=newAdomdConnection(connectionString);if(_connection !=null)if(_connection.State ==ConnectionState.Closed)
_connection.Open();
AdomdCommand command=_connection.CreateCommand();
StringBuilder sb=newStringBuilder();
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();varxmlreader =command.ExecuteXmlReader();
CellSet cellSet=CellSet.LoadXml(xmlreader);
_connection.Close();vardt =ToDataTable(cellSet);varv = dt.Rows.Count;
AdomdHelper.cs
usingSystem;usingSystem.Data;usingMicrosoft.AnalysisServices.AdomdClient;namespaceWpfapplication1
{///<summary>///Summary description for AdomdHelper.///</summary>publicclassAdomdHelper
{#region"== Enum ============================================================"publicenumVersions
{
Server,
Provider,
Client
}#endregion#region"== Methods ============================================================"//判断连接AdomdConnection对象是State是否处于Open状态。publicboolIsConnected(refAdomdConnection connection)
{return(!(connection ==null)) && (connection.State != ConnectionState.Broken) && (connection.State !=ConnectionState.Closed);
}///<summary>///断开连接///</summary>///<param name="connection">AdomdConnection对象的实例</param>///<param name="destroyConnection">是否销毁连接</param>publicvoidDisconnect(refAdomdConnection connection,booldestroyConnection)
{try{if(!(connection ==null))
{if(connection.State !=ConnectionState.Closed)
{
connection.Close();
}if(destroyConnection ==true)
{
connection.Dispose();
connection=null;
}
}
}catch(Exception ex)
{throwex;
}
}///<summary>///建立连接///</summary>///<param name="connection">AdomdConnection对象的实例</param>///<param name="connectionString">连接字符串</param>publicvoidConnect(refAdomdConnection connection,stringconnectionString)
{if(connectionString =="")thrownewArgumentNullException("connectionString","The connection string is not valid.");//Ensure an AdomdConnection object exists and that its ConnectionString property is set.if(connection ==null)
connection=newAdomdConnection(connectionString);else{
Disconnect(refconnection,false);
connection.ConnectionString=connectionString;
}try{
connection.Open();
}catch(Exception ex)
{throwex;
}
}///<summary>///获取OLAP数据库。///</summary>///<param name="connection">AdomdConnection对象的实例</param>///<param name="connectionString">连接字符串</param>///<returns></returns>publicDataTable GetSchemaDataSet_Catalogs(refAdomdConnection connection,stringconnectionString)
{boolconnected =true;//判断connection在调用此函数时,是否已经处于连接状态DataTable objTable =newDataTable();try{//Check if a valid connection was provided.if(IsConnected(refconnection) ==false)
{//如果连接不存在,则建立连接Connect(refconnection, connectionString);
connected=false;//更改connection为未连接状态。}
objTable= connection.GetSchemaDataSet(AdomdSchemaGuid.Catalogs,null).Tables[0];if(connected ==false)
{//关闭连接Disconnect(refconnection,false);
}
}catch(Exception err)
{throwerr;
}returnobjTable;
}///<summary>///通过SchemaDataSet的方式获取立方体///</summary>///<param name="connection">AdomdConnection对象的实例</param>///<param name="connectionString">连接字符串</param>///<returns></returns>publicstring[] GetSchemaDataSet_Cubes(refAdomdConnection connection,stringconnectionString)
{string[] strCubes =null;boolconnected =true;//判断connection是否已与数据库连接DataTable objTable =newDataTable();if(IsConnected(refconnection) ==false)
{try{
Connect(refconnection, connectionString);
connected=false;
}catch(Exception err)
{throwerr;
}
}string[] strRestriction =newstring[] {null,null,null};
objTable= connection.GetSchemaDataSet(AdomdSchemaGuid.Cubes, strRestriction).Tables[0];if(connected ==false)
{
Disconnect(refconnection,false);
}
strCubes=newstring[objTable.Rows.Count];introwcount =0;foreach(DataRow tempRowinobjTable.Rows)
{
strCubes[rowcount]= tempRow["CUBE_NAME"].ToString();
rowcount++;
}returnstrCubes;
}///<summary>///通过SchemaDataSet的方式获取制定立方体的维度///</summary>///<param name="connection">AdomdConnection对象的实例</param>///<param name="connectionString">连接字符串</param>///<returns></returns>publicstring[] GetSchemaDataSet_Dimensions(refAdomdConnection connection,stringconnectionString,stringcubeName)
{string[] strDimensions =null;boolconnected =true;//判断connection是否已与数据库连接DataTable objTable =newDataTable();if(IsConnected(refconnection) ==false)
{try{
Connect(refconnection, connectionString);
connected=false;
}catch(Exception err)
{throwerr;
}
}string[] strRestriction =newstring[] {null,null, cubeName,null,null};
objTable= connection.GetSchemaDataSet(AdomdSchemaGuid.Dimensions, strRestriction).Tables[0];if(connected ==false)
{
Disconnect(refconnection,false);
}
strDimensions=newstring[objTable.Rows.Count];introwcount =0;foreach(DataRow tempRowinobjTable.Rows)
{
strDimensions[rowcount]= tempRow["DIMENSION_NAME"].ToString();
rowcount++;
}returnstrDimensions;
}///<summary>///以connection的方式获取立方体///</summary>///<param name="connection">AdomdConnection对象的实例</param>///<param name="connectionString">连接字符串</param>///<returns></returns>publicstring[] GetCubes(refAdomdConnection connection,stringconnectionString)
{string[] strCubesName =null;boolconnected =true;//判断connection是否已与数据库连接if(IsConnected(refconnection) ==false)
{try{
Connect(refconnection, connection.ConnectionString);
connected=false;
}catch(Exception err)
{throwerr;
}
}introwcount =connection.Cubes.Count;
strCubesName=newstring[rowcount];for(inti =0; i < rowcount; i++)
{
strCubesName[i]=connection.Cubes[i].Caption;
}if(connected ==false)
{
Disconnect(refconnection,false);
}returnstrCubesName;
}///<summary>///获取立方体的维度///</summary>///<param name="connection">AdomdConnection对象的实例</param>///<param name="connectionString">连接字符串</param>///<param name="CubeName">立方体名称</param>///<returns></returns>publicstring[] GetDimensions(refAdomdConnection connection,stringconnectionString,stringCubeName)
{string[] strDimensions =null;boolconnected =true;if(IsConnected(refconnection) ==false)
{try{
Connect(refconnection, connection.ConnectionString);
connected=false;
}catch(Exception err)
{throwerr;
}
}introwcount =connection.Cubes[CubeName].Dimensions.Count;
strDimensions=newstring[rowcount];for(inti =0; i < rowcount; i++)
{
strDimensions[i]=connection.Cubes[CubeName].Dimensions[i].Caption.ToString();
}if(connected ==false)
{
Disconnect(refconnection,false);
}returnstrDimensions;
}#endregion}
}
View Code
代码下载
文章参考
使用 ADOMD.NET 进行开发使用ADOMD.NET建立与Analysis Services的连接