Click here to Skip to main content
15,881,735 members
Articles / Web Development / ASP.NET

Tree utilities in SQL Server 2000 and 2005, and OLAP implementations

Rate me:
Please Sign up or sign in to vote.
4.72/5 (25 votes)
19 Jul 2006CPOL11 min read 154.2K   3.3K   107  
This article describes how to efficiently store and access tree structures in a SQL Server database, and how to use them in OLAP implementations.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using DSO;

namespace RefreshSalesCube
{
	/// <summary>
	/// Summary description for RefreshSalesCube.
	/// </summary>
	class RefreshSalesCube
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			try
			{
				Console.WriteLine("Processing cube Sales...");
				DSO.ServerClass dsoServer = new ServerClass();
				dsoServer.Connect("localhost");

				DSO.OlapCollection coll = (DSO.OlapCollection)dsoServer.MDStores;
				DSO.Database dsoDB = (DSO.Database)coll.Item("TREE_DB");
	
				coll = (DSO.OlapCollection)dsoDB.MDStores;
				DSO.Cube dsoCube = (DSO.Cube)coll.Item("Sales");
	
				coll = (DSO.OlapCollection)dsoDB.Dimensions;
				DSO.Dimension dsoDim = (DSO.Dimension)coll.Item("TREE_OLAP");
				DSO.Dimension dsoTimeDim = (DSO.Dimension)coll.Item("TIME");

				dsoCube.Dimensions.Remove(dsoDim.Name);

				int i = dsoDim.Levels.Count;
				while(i > 0)
				{
					dsoDim.Levels.Remove(i);
					i--;
				}

				SqlConnection cnn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
				cnn.Open();

				SqlDataAdapter da = new SqlDataAdapter("SELECT TOP 0 * FROM TREE_OLAP", cnn);
				DataTable dt = new DataTable();
				da.Fill(dt);
				cnn.Close();

				int nColumns = dt.Columns.Count;
				int adVarChar = 200;
				for(i = 1; i < nColumns; i++)
				{
					DSO.Level lvl = (DSO.Level)dsoDim.Levels.AddNew(dt.Columns[i].ColumnName, DSO.SubClassTypes.sbclsRegular);
					lvl.MemberKeyColumn = "\"dbo\".\"TREE_OLAP\".\"" + dt.Columns[i].ColumnName + "\"";
					lvl.ColumnType = (short)adVarChar;
					lvl.ColumnSize = 100;
				}
				dsoDim.Process(DSO.ProcessTypes.processFull);

				/*
				object o = dsoCube.Dimensions.Count - 1;
				dsoCube.Dimensions.Add(dsoDim, "TREE_OLAP", o); // error "Type mismatch" at "Before" parameter
				*/

				DSO.Dimension dim = (DSO.Dimension)dsoCube.Dimensions.AddNew("TREE_OLAP", DSO.SubClassTypes.sbclsRegular);
				dim = dsoDim;

				dsoCube.JoinClause = "\"dbo\".\"Sales\".\"TREE_ID\" = \"dbo\".\"TREE_OLAP\".\"ID\" AND \"dbo\".\"Sales\".\"TIME_ID\" = \"dbo\".\"TIME_BY_DAY\".\"ID\"";

				dsoCube.Update();

				dsoCube.Process(DSO.ProcessTypes.processFull);

				dsoServer.CloseServer();

				Console.WriteLine("Sales cube was processed successfully!");
				Console.ReadLine();
			}
			catch(Exception ex)
			{
				while(ex != null)
				{
					Console.WriteLine(ex.Message);
					ex = ex.InnerException;
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer Telstra Internet
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions