我们如何使用C#代码通过ASP.Net处理多维数据集或访问OLAP数据库? C#.Net中用于连接OLAP数据库或anaysis Services中的流程操作的组件是什么?
答案 0 :(得分:12)
要进行处理,请使用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
答案 1 :(得分:1)
您必须处理数据库,而不是多维数据集。因为立方体只有尺寸而不是尺寸。这可能会产生一些冲突。
要Prozess all,Cubes和Dimensions,您必须处理整个数据库:
Server server = new Server();
server.Connect(cubeConnectionString);
Database database = server.Databases.FindByName(databaseName);
database.Process(ProcessType.ProcessFull);
答案 2 :(得分:0)
上面已经分享了这个问题的答案,但只是分享了我也使用过相同的Microsoft.AnalysisServices API,参考从here下载的示例项目来处理来自C#的多维数据集,但当维度数据发生变化时那么你需要处理数据库而不是立方体。
当必须在服务器上模拟最终用户身份时,您还可以使用连接字符串的EffectiveUserName属性。
注意:要使用EffectiveUserName属性,调用方必须具有Analysis Services中的管理权限。
答案 3 :(得分:-2)
这个例子是用Visual Studio Express 2012和Ms SQL 2012的44美元副本完成的(上帝保佑微软为这么少的钱提供了如此多的功能)。操作系统是Win 8专业版。
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;
}
}