日期:2014-05-17  浏览次数:20910 次

C#调用ADOMD的简单例子

最新在研究MDX,使用.net的C#语言调用ADOMD的单例子,

注意:在使用ADOMD时要导相应应的dll(Microsoft.AnalysisServices.AdomdClient,Microsoft.AnalysisServices.MdxCodeDom)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.AnalysisServices.MdxCodeDom;
using Microsoft.AnalysisServices.AdomdClient;

namespace mdx
{
??? public partial class Form1 : Form
??? {
??????? public Form1()
??????? {
??????????? InitializeComponent();
??????? }

??????? private DataTable ConvertMdxToDataTable(CellSet cs)
??????? {
??????????? DataTable dt = new DataTable();
??????????? dt.TableName = "reultsetTable";
??????????? DataColumn dc = null;
??????????? DataRow dr = null;
??????????? dt.Columns.Add(new DataColumn("描述"));
??????????? string name;
??????????? foreach (Position p in cs.Axes[0].Positions)
??????????? {
??????????????? name = "";
??????????????? dc = new DataColumn();
??????????????? MessageBox.Show(Convert.ToString(p.Members.Count));
??????????????? foreach (Member m in p.Members)
??????????????? {
??????????????????? name = name + m.Caption + " ";
???????????????????
??????????????? }
???????????????
??????????????? dc.ColumnName = name;
??????????????? dt.Columns.Add(dc);
??????????? }
??????????? int pos = 0;
??????????? foreach (Position py in cs.Axes[1].Positions)
??????????? {
??????????????? dr = dt.NewRow();
??????????????? name = "";
??????????????? foreach (Member m in py.Members)
??????????????? {
??????????????????? name = name + m.Caption + " ";
??????????????? }
??????????????? dr[0] = name;
??????????????? for (int x = 1; x <= cs.Axes[0].Positions.Count; x++)
??????????????? {
??????????????????? dr[x] = cs[pos++].FormattedValue;
??????????????? }
??????????????? dt.Rows.Add(dr);
??????????? }
??????????? return dt;
??????? }
??????? private void button1_Click(object sender, EventArgs e)
??????? {
??????????? string connectionString = "Data Source=localhost;Initial Catalog=foodmartdw";
??????????? AdomdConnection connection;
??????????? connection = new AdomdConnection(connectionString);
??????????? connection.Open();
??????????? AdomdCommand command;
??????????? string mdx = "select {Measures.[Unit Sales]}? on columns, "
???????????? +" exists([Store].[Store Name].members,{[Store].[Store Type].&[Supermarket]})ON ROWS "
???????????? +" FROM foodmartsaledw ";
??????????? command = new AdomdCommand(mdx, connection);
??????????? CellSet cellSet = command.ExecuteCellSet();
??????????? connection.Close();
??????????? DataTable datatable = ConvertMdxToDataTable(cellSet);
??????????? bindingSource1.DataSource = datatable;
??????????? dataGridView1.DataSource = bindingSource1;
??????? }
??? }
}