2010-05-06 87 views

回答

11

進行處理,使用Microsoft.AnalysisServices庫,例如代碼如下:

Server server = new Server(); 
    server.Connect(cubeConnectionString); 

    Database database = server.Databases.FindByName(databaseName); 
    Cube cube = database.Cubes.FindByName(cubeName); 

    cube.Process(ProcessType.ProcessFull); 

對於查詢,使用Microsoft.AnalysisServices.AdomdClient庫,例如代碼如下:

using (Adomd.AdomdConnection adomdConnection = new Microsoft.AnalysisServices.AdomdClient.AdomdConnection()) 
    { 
     adomdConnection.ConnectionString = cubeConnectionString; 
     Adomd.AdomdCommand adomdCommand = new Microsoft.AnalysisServices.AdomdClient.AdomdCommand(); 
     adomdCommand.Connection = adomdConnection; 
     adomdCommand.CommandText = mdxQuery; 
     adomdConnection.Open(); 
     cellSet = adomdCommand.ExecuteCellSet(); 
     adomdConnection.Close(); 
    } 

注意這兩個名稱空間是重疊的,所以如果你在同一個地方使用它們,你可能需要別名。

http://msdn.microsoft.com/en-US/library/ms124924(v=SQL.90).aspx

http://msdn.microsoft.com/en-us/library/ms123483(v=SQL.90).aspx

+0

感謝您的答覆 – 2010-05-07 05:14:36

+1

我不認爲你需要adomdConnection.Close();因爲你使用using語句。不過,我認爲你應該adomdCommand.Dispose();因爲adomdCommand不在使用聲明中,但我不確定。 – user3711421 2016-01-04 12:32:04

+0

@Meff,你的「服務器」變量正在使用什麼樣的庫? – 2017-07-13 14:19:27

-2

這個例子是用Visual Studio Express的2012做,SQL 2012女士$ 44副本(上帝保佑微軟爲這麼點錢提供這麼多的功能)。該操作系統是Win 8 pro。

using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
using System.Threading.Tasks; 
//the next 2 using's had to be downloaded and "Add Reference"d for Visual Studio Express 2012 
using Microsoft.AnalysisServices; 
using Microsoft.AnalysisServices.AdomdClient; 
using System.Windows.Forms; 
using System; 
using System.Data; 
using System.Drawing; 
namespace SSASDataview 
{ 
     partial class Form1 
    { 
    /// <summary> 
    /// Required designer variable. 
    /// </summary> 
    private System.ComponentModel.IContainer components = null; 

    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 
    private void RunSSAS(object sender, EventArgs e) 
    { 
     //i don't think Dataset is in the Analysis Services directives 
     DataSet ds = new DataSet(); 
     // provider is the constant olap. datasource is the same server name you provide for Mgmt Studio or localhost 
     // initial catalog is tricky and important. It is not a standard ms sql database you see in Management Studio, 
     // even if your cube was create with tables from a particular database. 
     // the only place I was able to see "initial catalog" value was a File -> Open -> Analysis Services Database in 2012 Management Studio 
     // it was also the name of the VS2010 solution I used to create the cube. 
     AdomdConnection myconnect = new AdomdConnection(@"provider=olap;initial catalog=GLCubeThree;datasource=localhost"); 
     AdomdDataAdapter mycommand = new AdomdDataAdapter(); 
     mycommand.SelectCommand = new AdomdCommand(); 
     mycommand.SelectCommand.Connection = myconnect; 
     // this query was created by the "Browser" you see for an Analysis Services project 
     // if you poke around the icons on the browser table the Design Mode icon will give you the cube query 
     // I think it's an MDX query, threre are also xml queries you can run with adomd 
     mycommand.SelectCommand.CommandText = "SELECT NON EMPTY { [Measures].[Per Balance] } ON COLUMNS, NON EMPTY { ([Gltime].[Fisc Per].[Fisc Per].ALLMEMBERS) } DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM (SELECT ({ [Gltime].[Fisc Per].&[201301], [Gltime].[Fisc Per].&[201302], [Gltime].[Fisc Per].&[201307] }) ON COLUMNS FROM [GL Cube]) CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS"; 
     myconnect.Open(); 
     mycommand.Fill(ds, "tbl"); 
     myconnect.Close(); 
     // the below assigns the results of the cube query to a dataGridView 
     // if you drag a dataGridView control to your pallete it will create exactly 
     // what you need for the line below to work. 
     // your project type has to be a Window Forms Applications 
     // this code shown here is in the default Form1.Designer.cs not Form1.cs 
     dataGridView1.DataSource = new DataView(ds.Tables[0]); 


    } 
    private void Quit_Click(object sender, EventArgs e) 
    { 
     this.Close(); 
    } 

    #region Windows Form Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 

    #endregion 

    private System.Windows.Forms.DataGridView dataGridView1; 
    private System.Windows.Forms.Button runssas; 
    private System.Windows.Forms.Button quit; 
} 

}

+0

是否有更新版本的ADOMD.NET?我唯一能找到的就是[this](http://www.microsoft.com/en-us/download/details.aspx?id=23089),但是這是上傳到2005 – 2013-06-08 04:35:12

+1

,這裏沒有關於多維數據集處理的內容。 – 2013-10-07 08:23:03

0

回答這個上面已經共享,但公正地分享,我也曾經使用的API相同Microsoft.AnalysisServices指從here下載示例項目從C#處理立方體,但當維數據被改變,那麼你需要處理數據庫而不是立方體

當服務器上必須模擬最終用戶身份時,您還可以使用連接字符串的EffectiveUserName屬性。

NOTE:要使用EffectiveUserName屬性,調用方必須在Analysis Services中具有管理權限。

1

您必須處理數據庫,而不是多維數據集。因爲立方體只有尺寸不是內部的尺寸。這可能會導致一些衝突。

要Prozess所有,多維數據集和維度,您必須處理整個數據庫:

Server server = new Server(); 
server.Connect(cubeConnectionString); 

Database database = server.Databases.FindByName(databaseName); 

database.Process(ProcessType.ProcessFull);