萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mssql數據庫 >> 通過AMO獲取SQL Server SSAS信息

通過AMO獲取SQL Server SSAS信息

   Analysis Management Objects (AMO) 是SQL Server SSAS的對象模型庫,通過它可以方便的對SSAS裡的對象進行訪問及控制,包括Cube,DataSource, DataSourceView, Partition, Measure, Dimension, Assembly, Role以及DataMining對象等。要使用它,必須在機器上找到SSAS的安裝路徑MicrosoftSQL Server90SDKAssemblies,把目錄中的Microsoft.AnalysisServices.Dll文件加載到項目的Reference列表中,AMO對象就是通過這個Dll文件進行訪問的。

  首先要添加引用using Microsoft.AnalysisServices;

  以下是測試用C#從SSAS中獲取部分信息的源碼:(代碼中已經包含了較多的解釋)

  using System;

  using System.Collections.Generic;

  using System.Linq;

  using System.Text;

  using System.Threading.Tasks;

  using Microsoft.AnalysisServices;

  namespace AMOTest

  {

  class Program

  {

  static void Main(string[] args)

  {

  string ConnecteString = "Data Source =ServerName; Provider=msolap";

  Server SSASServer = new Server();

  SSASServer.Connect(ConnecteString);

  List OutDatabase = GetDatabaseCollection(SSASServer);

  //show all database on the server

  Console.WriteLine("-------------Databse-------------------------------");

  for (int i = 0; i < OutDatabase.Count; i++)

  {

  Console.WriteLine(OutDatabase[i].Name.ToString());

  }

  //show the Cube of a database

  Console.WriteLine("---------------Cube--------------------------------");

  List OutCube = GetCubeCollection(OutDatabase[0]);

  for (int i = 0; i < OutCube.Count; i++)

  {

  Console.WriteLine(OutCube[i].Name.ToString());

  }

  //show all Roles of one database

  Console.WriteLine("---------------Roles_database-------------------------");

  List OutRole = GetRolescollection(OutDatabase[0]);

  Console.WriteLine(OutRole[0].Members.Count);

  //for (int i = 0; i < OutRole[0].Members.Count; i++) //to show the detial of the role Members

  //{

  // Console.WriteLine(OutRole[0].Members[i].Name);

  //}

  //show all Roles of one cube

  Console.WriteLine("---------------Roles_Cube----------------------------");

  List OutRole2 = GetRolescollection2(OutCube[0]);

  Console.WriteLine(OutRole2[0].Members.Count);

  //for (int i = 0; i < OutRole2[0].Members.Count; i++)

  //{

  // Console.WriteLine(OutRole2[0].Members[i].Name); //to show the detial of the role Members

  //}

  Console.Read();

  }

  //get the list of the database name

  static public List GetDatabaseCollection(Server server)

  {

  List collectdb = new List();

  foreach (Database db in server.Databases)

  {

  collectdb.Add(db);

  }

  return collectdb;

  }

  //get the list of a database cube name

  static public List GetCubeCollection(Database db)

  {

  List collectcube = new List();

  foreach (Cube cube in db.Cubes)

  {

  collectcube.Add(cube);

  }

  return collectcube;

  }

  //get the list of the database roles

  static public List GetRolescollection(Database db)

  {

  List collectRoles = new List();

  foreach (Role cp in db.Roles)

  {

  collectRoles.Add(cp);

  }

  return collectRoles;

  }

  //get the list of the cube Roles

  static public List GetRolescollection2(Cube cube)

  {

  List collectionRoles = new List();

  foreach (CubePermission cp in cube.CubePermissions)

  {

  collectionRoles.Add(cp.Role);

  }

  return collectionRoles;

  }

  }

  }

  這裡需要說明的是,當我獲取Database的Roles和Cube的Roles值時,做了一個對比,他們的值是相同的。這可能的情況是,在Database中Cube只有一個,或者因為Cube隸屬於database,他們的Roles值是相同的。

  如下是運行完的截圖:

通過AMO獲取SQL Server SSAS信息 三聯
copyright © 萬盛學電腦網 all rights reserved