【问题标题】:.NET connecting to SSAS.NET 连接到 SSAS
【发布时间】:2019-03-26 14:42:25
【问题描述】:

希望有人能回答这个问题:所以我想开始一个项目,其中 .NET 解决方案连接到 SSAS(SQL Server 分析服务)。但是在互联网上只有很少的主题或指南。 据我所知,我可以使用名为 ADOMD.NET 的 C# 库。但我不知道这对我来说是否足够。 假设我将在数据库中拥有数百万条记录,为了获得一些分析记录,我需要拥有能够创建表格模型的 SSAS(据我所知,“数据库”记录存储在内存中)。 所以问题是:我是否能够使用 ADOMD.NET 使用特定语言(如果存在)创建表格模型,你们中的一些人是否有一些建议?因为我需要将内存中存储的数据返回给用户并显示出来。

【问题讨论】:

    标签: c# .net ssas tabular


    【解决方案1】:

    由于您使用的是 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);
    }
    

    【讨论】:

    • Microsoft.AnalysisServices.dll 的版本是多少?我正在使用 15.0。但是这个版本没有Connect()方法。
    猜你喜欢
    • 1970-01-01
    • 2011-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2021-11-15
    • 1970-01-01
    相关资源
    最近更新 更多