欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中使用ADOMD.NET查询多维数据集的实现方法

程序员文章站 2023-12-16 09:42:04
本文实例讲述了c#中使用adomd.net查询多维数据集的实现方法,分享给大家供大家参考。具体实现方法分析如下: adomd.net 是用于与 microsoft sql...

本文实例讲述了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#程序设计有所帮助。

上一篇:

下一篇: