Guest User

Untitled

a guest
Apr 26th, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.AnalysisServices;
  6. using Microsoft.AnalysisServices.Hosting;
  7. using Microsoft.AnalysisServices.AdomdClient;
  8. using System.IO;
  9. using Newtonsoft.Json;
  10. using System.Xml;
  11. using System.Security.Principal;
  12.  
  13. namespace ConsoleApplication1
  14. {
  15.     class ConsoleApplication1
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.  
  20.             String ConnStr;
  21.  
  22.             String OLAPServerName;
  23.             String OLAPDB;
  24.             String OLAPCube;
  25.             String mdxQuery;
  26.             CellSet cst;
  27.             Database CubeDB;
  28.             String filePath;
  29.  
  30.  
  31.             OLAPServerName = args[0];
  32.             OLAPCube = args[1];
  33.             mdxQuery = args[2];
  34.  
  35.  
  36.             ConnStr = "Provider=MSOLAP;Data Source=" + OLAPServerName + ";";
  37.             //Initial Catalog=Adventure Works DW 2008R2;";
  38.  
  39.             using (WindowsIdentity.GetCurrent().Impersonate())
  40.             {
  41.                 Server OLAPServer = new Server();
  42.                 OLAPServer.Connect(ConnStr);
  43.  
  44.                 filePath = AppDomain.CurrentDomain.BaseDirectory + @"\tmp\" + System.IO.Path.GetRandomFileName() + ".json";
  45.  
  46.                 CubeDB = OLAPServer.Databases.GetByName(OLAPCube);
  47.  
  48.                 CubeDB.Process(ProcessType.ProcessFull);
  49.  
  50.                 using (AdomdConnection adomdConnection = new Microsoft.AnalysisServices.AdomdClient.AdomdConnection())
  51.                 {
  52.                     adomdConnection.ConnectionString = ConnStr;
  53.                     AdomdCommand adomdCommand = new Microsoft.AnalysisServices.AdomdClient.AdomdCommand();
  54.                     adomdCommand.Connection = adomdConnection;
  55.                     adomdCommand.CommandText = mdxQuery;
  56.                     adomdConnection.Open();
  57.                     System.Xml.XmlReader reader = adomdCommand.ExecuteXmlReader();
  58.                     String xml = reader.ReadOuterXml();
  59.                     XmlDocument doc = new XmlDocument();
  60.                     doc.LoadXml(xml);
  61.                     string json = JsonConvert.SerializeXmlNode(doc);
  62.                     System.IO.File.WriteAllText(filePath, json);
  63.  
  64.                     reader.Close();
  65.                     adomdConnection.Close();
  66.                 }
  67.             }
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment