En seguida el listado de código
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Microsoft.AnalysisServices.AdomdClient;
namespace F.Data.MSAnalisysServices
{
/// <summary>
/// MS-OLAP Repository Class Handler
/// </summary>
public class Repository
{
private string _cnstr;
/// <summary>
/// ConnectionString for the AdomdConnection object
/// </summary>
public string ConnectionString
{
get
{
return _cnstr;
}
set
{
_cnstr = value;
if (_cn == null)
{
_cn = new AdomdConnection();
}
_cn.ConnectionString = _cnstr;
}
}
private AdomdConnection _cn;
/// <summary>
/// AdomdConnection Object
/// </summary>
public AdomdConnection Connection
{
get
{
if (_cn == null)
_cn = new AdomdConnection(_cnstr);
Open();
return _cn;
}
set
{
_cn = value;
}
}
/// <summary>
///
/// </summary>
public CubeCollection Cubes
{
get
{
if (_cn != null)
{
return _cn.Cubes;
}
return null;
}
}
string _mdx;
/// <summary>
/// Property to set/get the MDX Query
/// </summary>
public string QueryMDX
{
get
{
return _mdx;
}
set
{
_mdx = value;
}
}
/// <summary>
/// Default Cube Constructor
/// </summary>
public Repository(string connectionString)
{
_cnstr = connectionString;
}
/// <summary>
/// Overriden constructor initialice the connection string and mdx query
/// </summary>
/// <param name="connectionString">connection string</param>
/// <param name="MDXqry">MDX Query</param>
public Repository(string connectionString, string MDXqry)
{
_cnstr = connectionString;
_mdx = MDXqry;
}
/// <summary>
///
/// </summary>
/// <param name="cubeName"></param>
/// <returns></returns>
public DimensionCollection Dimensions(string cubeName)
{
if (_cn != null)
{
CubeDef c;
c = _cn.Cubes.Find(cubeName);
if (c != null)
{
return c.Dimensions;
}
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="cubeName"></param>
/// <param name="dimension"></param>
/// <returns></returns>
public Dimension Dimension(string cubeName, string dimension)
{
DimensionCollection dc = Dimensions(cubeName);
if (dc != null)
{
return dc.Find(dimension);
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="cubeName"></param>
/// <returns></returns>
public MeasureCollection Measures(string cubeName)
{
if (_cn != null)
{
CubeDef c;
c = _cn.Cubes.Find(cubeName);
if (c != null)
{
return c.Measures;
}
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="cubeName"></param>
/// <param name="measureName"></param>
/// <returns></returns>
public Measure Measure(string cubeName, string measureName)
{
MeasureCollection mc = Measures(cubeName);
if (mc != null)
{
return mc.Find(measureName);
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="cubeName"></param>
/// <returns></returns>
public HierarchyCollection Hierarchies(string cubeName, string dimension)
{
if (_cn != null)
{
CubeDef c;
c = _cn.Cubes.Find(cubeName);
if (c != null)
{
return c.Dimensions[dimension].Hierarchies;
}
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="cubeName"></param>
/// <param name="measureName"></param>
/// <returns></returns>
public Hierarchy Hierarchy(string cubeName, string dimension, string name)
{
HierarchyCollection hc = Hierarchies(cubeName, dimension);
if (hc != null)
{
return hc.Find(name);
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="cubeName"></param>
/// <returns></returns>
public NamedSetCollection NamedSets(string cubeName)
{
if (_cn != null)
{
CubeDef c;
c = _cn.Cubes.Find(cubeName);
if (c != null)
{
return c.NamedSets;
}
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="cubeName"></param>
/// <param name="setName"></param>
/// <returns></returns>
public NamedSet NamedSet(string cubeName, string setName)
{
NamedSetCollection nc = NamedSets(cubeName);
if (nc != null)
{
return nc.Find(setName);
}
return null;
}
public LevelCollection Levels(string cubeName, string dimension, string hierarchy)
{
if (_cn != null)
{
CubeDef c;
c = _cn.Cubes.Find(cubeName);
if (c != null)
{
return c.Dimensions[dimension].Hierarchies[hierarchy].Levels;
}
}
return null;
}
public Level Level(string cubeName, string dimension, string hierarchy, string level)
{
LevelCollection lc = Levels(cubeName, dimension, hierarchy);
if (lc != null)
{
return lc.Find(level);
}
return null;
}
public MemberCollection Members(string cubeName, string dimension, string hierarchy, string level)
{
if (_cn != null)
{
CubeDef c;
c = _cn.Cubes.Find(cubeName);
if (c != null)
{
return c.Dimensions[dimension].Hierarchies[hierarchy].Levels[level].GetMembers();
}
}
return null;
}
public Member Member(string cubeName, string dimension, string hierarchy, string level, string member)
{
MemberCollection mc = Members(cubeName, dimension, hierarchy, level);
if (mc != null)
{
return mc.Find(member);
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="cubeName"></param>
/// <returns></returns>
public CubeDef Cube(string cubeName)
{
if (_cn != null)
{
return _cn.Cubes.Find(cubeName);
}
return null;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public ArrayList ListOfCubes()
{
if (_cn != null)
{
ArrayList a = new ArrayList();
foreach (CubeDef c in _cn.Cubes)
{
a.Add(c.Name);
}
return a;
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="cubeName"></param>
/// <returns></returns>
public ArrayList ListOfDimensions(string cubeName)
{
if (_cn != null)
{
ArrayList a = new ArrayList();
foreach (Dimension d in Dimensions(cubeName))
{
a.Add(d.Name);
}
return a;
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="cubeName"></param>
/// <returns></returns>
public ArrayList ListOfMeasures(string cubeName)
{
if (_cn != null)
{
ArrayList a = new ArrayList();
foreach (Measure m in Measures(cubeName))
{
a.Add(m.Name);
}
return a;
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="cubeName"></param>
/// <returns></returns>
public ArrayList ListOfNamedSets(string cubeName)
{
if (_cn != null)
{
ArrayList a = new ArrayList();
foreach (NamedSet n in NamedSets(cubeName))
{
a.Add(n.Name);
}
return a;
}
return null;
}
/// <summary>
///
/// </summary>
/// <param name="cubeName"></param>
/// <param name="dimension"></param>
/// <returns></returns>
public ArrayList ListOfHierarchies(string cubeName, string dimension)
{
if (_cn != null)
{
ArrayList a = new ArrayList();
foreach (Hierarchy h in Hierarchies(cubeName, dimension))
{
a.Add(h.Name);
}
return a;
}
return null;
}
/// <summary>
///
///
/// </summary>
/// <param name="cubeName"></param>
/// <param name="dimension"></param>
/// <param name="hierachy"></param>
/// <returns></returns>
public ArrayList ListOfLevels(string cubeName, string dimension, string hierachy)
{
if (_cn != null)
{
ArrayList a = new ArrayList();
foreach (Level l in Levels(cubeName, dimension, hierachy))
{
a.Add(l.Name);
}
return a;
}
return null;
}
public ArrayList ListOfMembers(string cubeName, string dimension, string hierachy, string level)
{
if (_cn != null)
{
ArrayList a = new ArrayList();
foreach (Member m in Members(cubeName, dimension, hierachy, level))
{
a.Add(m.Name);
}
return a;
}
return null;
}
/// <summary>
/// Executes a MDX query an get the results into a DataSet object
/// </summary>
/// <param name="qry">MDX Query</param>
/// <returns></returns>
public DataSet ToDataSet(string qry)
{
_mdx = qry;
return ToDataSet();
}
/// <summary>
/// Executes a MDX query an get the results into a DataSet object
/// </summary>
/// <returns></returns>
public DataSet ToDataSet()
{
DataSet DS = new DataSet();
AdomdDataAdapter DA;
try
{
DA = new AdomdDataAdapter(_mdx, Connection);
DA.Fill(DS);
}
catch (AdomdException ex1)
{
DS = null;
throw ex1;
}
catch (Exception ex)
{
DS = null;
throw ex;
}
return DS;
}
/// <summary>
///
/// </summary>
/// <param name="qry"></param>
/// <returns></returns>
public DataTable ToDataTable(string qry)
{
_mdx = qry;
return ToDataTable();
}
/// <summary>
/// Executes a MDX query an get the results into a DataTable object
/// </summary>
/// <returns></returns>
public DataTable ToDataTable()
{
DataSet DS = new DataSet();
DataTable DT = null;
AdomdDataAdapter DA;
string tname = "T1";
try
{
DA = new AdomdDataAdapter(_mdx, Connection);
DA.Fill(DS, tname);
if (DS != null)
{
if (DS.Tables.Contains(tname))
{
DT = DS.Tables[tname];
}
}
}
catch (AdomdException ex1)
{
DT = null;
throw ex1;
}
catch (Exception ex)
{
DT = null;
throw ex;
}
return DT;
}
/// <summary>
///
/// </summary>
public void Close()
{
try
{
if (_cn != null)
{
if (_cn.State != ConnectionState.Closed)
{
_cn.Close();
}
}
}
catch
{
}
}
/// <summary>
/// Open the AdomdConnection object called Connection
/// </summary>
public void Open()
{
try
{
if (_cn == null)
{
_cn = new AdomdConnection();
}
if (_cn != null)
{
_cn.ConnectionString = _cnstr;
_cn.Open();
}
}
catch
{
}
}
}
}