C# Adomd Connection Analysis Service View Cube

时间:2023-03-09 12:49:11
C# Adomd Connection Analysis Service View Cube
首先需要先引用 C:\Program Files\Microsoft.NET\ADOMD.NET\100\Microsoft.AnalysisServices.AdomdClient.dll
                        C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies\Microsoft.AnalysisServices.DLL           
参照来源为MSDN:http://gallery.technet.microsoft.com/scriptcenter/6901ede5-5d59-4b1f-8f1b-df3c8e645048#content

-------------------------------------------------------------------------------------------------------------------------------------
            using Microsoft.AnalysisServices;
            using Microsoft.AnalysisServices.Hosting;

String ConnStr;

            String OLAPServerName;
            String OLAPDB;
            String OLAPCube;
            OLAPServerName = "HZSV008";
            OLAPDB = "TajimaSalesSubject";
            OLAPCube = "销售主题分析模型";
            ConnStr = "Provider=MSOLAP;Data Source=" + OLAPServerName + ";";
            //Initial Catalog=Adventure Works DW 2008R2;"; 
            Server OLAPServer = new Server();
            OLAPServer.Connect(ConnStr);
            Console.WriteLine("ServerName : " + OLAPServerName);
            // Database 
            foreach (Database OLAPDatabase in OLAPServer.Databases)
            {
                if (OLAPDB == "" || OLAPDatabase.Name.ToString() == OLAPDB)
                {
                    Console.WriteLine("DatabaseName : " + OLAPDatabase.Name);
                    // Cube 
                    foreach (Cube OLAPCubex in OLAPDatabase.Cubes)
                    {
                        if (OLAPCube == "" || OLAPCubex.Name == OLAPCube)
                        {
                            Console.WriteLine("CubeName : " + OLAPCubex.Name);
                            //Cube Dimension 
                            foreach (CubeDimension OLAPDimension in OLAPCubex.Dimensions)
                            {
                                Console.WriteLine("DimensionName : " + OLAPDimension.Name);
                                Console.WriteLine("    Attributes : ");
                                //Dimension Attribute 
                                foreach (CubeAttribute OLAPDimAttribute in OLAPDimension.Attributes)
                                {
                                    Console.WriteLine("          " + OLAPDimAttribute.Attribute.Name);
                                }
                                Console.WriteLine("    Hierarchy : ");
                                //Dimension Hierarchy 
                                foreach (CubeHierarchy OLAPDimHierarchy in OLAPDimension.Hierarchies)
                                {
                                    Console.WriteLine("          " + OLAPDimHierarchy.Hierarchy.Name);
                                    //Dimension Hierarchy Level 
                                    foreach (Level OLAPDimHierachyLevel in OLAPDimHierarchy.Hierarchy.Levels)
                                    {
                                        Console.WriteLine("                " + OLAPDimHierachyLevel.Name);
                                    }
                                }
                                //Measure Group 
                                foreach (MeasureGroup OLAPMeasureGroup in OLAPCubex.MeasureGroups)
                                {
                                    Console.WriteLine("Measure Group:" + OLAPMeasureGroup.Name);
                                    Console.WriteLine("Measures:");
                                    // Measures 
                                    foreach (Measure OLAPMeasure in OLAPMeasureGroup.Measures)
                                    {
                                        Console.WriteLine("          " + OLAPMeasure.Name);
                                    }
                                    Console.WriteLine("Measure Group Dimension:");
                                    // Measure Group Dimension 
                                    foreach (MeasureGroupDimension OLAPMeasureGroupDimension in OLAPMeasureGroup.Dimensions)
                                    {
                                        Console.WriteLine("          " + OLAPMeasureGroupDimension.CubeDimension.Name);
                                    }
                                    Console.WriteLine("Partition:");
                                    //Partitions 
                                    foreach (Partition OLAPPartition in OLAPMeasureGroup.Partitions)
                                    {
                                        Console.WriteLine("          " + OLAPPartition.Name);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Console.ReadKey();