简体   繁体   中英

.NET connecting to SSAS

hopefully someone might answer the question: so I would like to start a project in which .NET solution is connected to SSAS (SQL Server Analysis Services). But on the internet there are only few topics or guides. As far as I know I can use C# library called ADOMD.NET. But I don't know if it's enough for me. Let's pretend I will have tons of millions of records in database, to get some analytics records of it I need to have SSAS which has a ability to create a tabular models("database" records stored in memory as much as I know). So the questions are: am I able with ADOMD.NET to create tabular models using specific language(if it's exists) and are there some recommendations from some of you have working experience of it? Because I need to return that in memory stored data to the user and show it.

Since you're working with SSAS Tabular, I'd recommend using Tabular Object Model (TOM) library, which is an extension of AMO for tabular models. Note that this is for compatibility level 1200 or above. The example below creates a basic model with a single dimension and fact table, one measure, and a relationship between them to filter the fact table from the dimension. After this, the new model is deployed to the SSAS server and then processed so it is available for use. In addition to Microsoft.AnalysisServices.Tabular, you'll also want to add references to Microsoft.AnalysisServices.Core and 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM