希望有人可能会回答这个问题:所以我想开始一个.NET解决方案连接到SSAS(SQL Server Analysis Services)的项目。但在互联网上只有很少的主题或指南。据我所知,我可以使用名为ADOMD.NET的C#库。但我不知道这对我来说是否足够。让我假装我将在数据库中拥有数百万条记录,以获得它的一些分析记录我需要SSAS能够创建表格模型(“数据库”记录存储在内存中,据我所知)。所以问题是:我能用ADOMD.NET创建使用特定语言的表格模型(如果它存在的话)并且你们中有些人有一些工作经验吗?因为我需要将内存中存储的数据返回给用户并显示它。
由于您正在使用SSAS表格,我建议使用Tabular Object Model(TOM)库,它是表格模型的AMO的扩展。请注意,这是针对兼容级别1200或更高级别。下面的示例创建一个基本模型,其中包含单个维度和事实表,一个度量以及它们之间的关系,以便从维度中过滤事实表。在此之后,新模型将部署到SSAS服务器,然后进行处理,以便可以使用。除了Microsoft.AnalysisServices.Tabular之外,您还需要添加对Microsoft.AnalysisServices.Core和Microsoft.AnalysisServices.AdomdClient的引用。
using Microsoft.AnalysisServices;
using Microsoft.AnalysisServices.Tabular;
string connStr = @"Data Source=ServerName";
string dataSource = "Data Source Name";
string measureExpression = @"SUM('FactTable'[Amount])";
using (Server serv = new Server())
{
serv.Connect(connStr);
string dbName = serv.Databases.GetNewName("New Tabular Model Name");
Database db = new Database()
{
Name = dbName,
ID = dbName,
CompatibilityLevel = 1200,
StorageEngineUsed = StorageEngineUsed.TabularMetadata
};
db.Model = new Model()
{
Name = "Model",
Description = "Model Description"
};
//define data source
db.Model.DataSources.Add(new ProviderDataSource()
{
Name = dataSource,
Description = "Data Source Description",
//for SQL server
ConnectionString = @"Provider=SQLNCLI11;Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=SSPI",
ImpersonationMode = Microsoft.AnalysisServices.Tabular.ImpersonationMode.ImpersonateAccount,
Account = @"AccountName",
Password = "Password",
});
//add tables
//dimension table
db.Model.Tables.Add(new Table()
{
Name = db.Model.Tables.GetNewName("DimTable"),
Description = "Dimension Table Description ",
Partitions = {
new Partition() {
Name = "Partition 1",
Source = new QueryPartitionSource() {
DataSource = db.Model.DataSources[dataSource],
Query = @"SELECT ID, NAME FROM DimensionTable",
}
}
},
Columns =
{
new DataColumn() {
Name = "ID",
DataType = DataType.Int64,
SourceColumn = "ID",
},
new DataColumn() {
Name = "Name",
DataType = DataType.String,
SourceColumn = "NAME",
},
}
});
//fact table
db.Model.Tables.Add(new Table()
{
Name = db.Model.Tables.GetNewName("FactTable"),
Description = "FactTable Description",
Partitions = {
new Partition() {
Name = "Partition 1",
Source = new QueryPartitionSource() {
DataSource = db.Model.DataSources[dataSource],
Query = @"SELECT ID, AMOUNT FROM FactTable",
}
}
},
Columns =
{
new DataColumn() {
Name = "ID",
DataType = DataType.Int64,
SourceColumn = "ID",
},
new DataColumn() {
Name = "Amount",
DataType = DataType.Int64,
SourceColumn = "AMOUNT",
},
}
});
//create column objects for relationship
Column fromColumn = db.Model.Tables["FactTable"].Columns["ID"];
Column toColumn = db.Model.Tables["DimTable"].Columns["ID"];
//create relationship to filter fact table
SingleColumnRelationship relationship = new SingleColumnRelationship()
{
Name = "FactTable_ID_DimTable_ID",
ToColumn = toColumn,
FromColumn = fromColumn,
ToCardinality = RelationshipEndCardinality.One,
FromCardinality = RelationshipEndCardinality.Many
};
db.Model.Relationships.Add(relationship);
//create measure
Measure measure = new Measure()
{ Name = "Total" };
db.Model.Tables["FactTable"].Measures.Add(measure);
measure.Expression = measureExpression;
serv.Databases.Add(db);
//deploy database to SSAS Server
db.Update(UpdateOptions.ExpandFull);
//process new model so it's available to query
db.Model.RequestRefresh(Microsoft.AnalysisServices.Tabular.RefreshType.Full);
db.Update(UpdateOptions.ExpandFull);
}