如何将带有代码的csv文件导入表格模型

时间:2019-05-13 05:11:19

标签: c# ssas-tabular import-csv

我正在设置一个Web服务,用户可以在其中上传csv文件,然后将其添加到SASS表格模型中。通过代码甚至有可能吗?如果是这样,怎么办?我知道您可以通过VS手动完成此操作,但是我需要自动执行。

可以进行变通,将csv文件上传到sql数据库,然后从那里以表格模式加载。但是,由于文件很大(〜500Mb),因此可能需要一段时间才能执行此操作。

我当前的代码,连接到SASS服务器并将数据从sql数据库复制到SASS

 public void Add()
        {
            string connStr = @"Data Source=localhost:64731";
            string dataSource = "AdventureWorksDW2017";
            string measureExpression = @"SUM('DimEployee'[Freq])";

            using (Server serv = new Server())
            {
                serv.Connect(connStr);

                string dbName = serv.Databases.GetNewName("TestBase3000");

                Database db = new Database()
                {
                    Name = dbName,
                    ID = dbName,
                    CompatibilityLevel = 1200,
                    StorageEngineUsed = StorageEngineUsed.TabularMetadata
                };

                db.Model = new Model()
                {
                    Name = "Tabular model test",
                    Description = "Test"
                };

                //define data source
                db.Model.DataSources.Add(new ProviderDataSource()
                {
                    Name = dataSource,
                    Description = "Aventure works",
                    //for SQL server
                    ConnectionString =
                        @"Provider=SQLNCLI11;Data Source=DESKTOP-K9RTS02\SQLEXPRESS;Initial Catalog=AdventureWorksDW2017;Integrated Security=SSPI",
                    ImpersonationMode = Microsoft.AnalysisServices.Tabular.ImpersonationMode.ImpersonateServiceAccount,
                });

                // add tables
                // dimension table
                db.Model.Tables.Add(new Table()
                {
                    Name = db.Model.Tables.GetNewName("DimProductSubcategory"),
                    Description = "Testing",
                    Partitions =
                    {
                        new Partition()
                        {
                            Name = "Partition 1",
                            Source = new QueryPartitionSource()
                            {
                                DataSource = db.Model.DataSources[dataSource],
                                Query = @"SELECT ProductSubcategoryKey, ProductSubcategoryAlternateKey, EnglishProductSubcategoryName FROM DimProductSubcategory",
                            }
                        }
                    },
                    Columns =
                    {
                        new Microsoft.AnalysisServices.Tabular.DataColumn()
                        {
                            Name = "Id",
                            DataType = DataType.Int64,
                            SourceColumn = "ProductSubcategoryKey",
                        },
                        new Microsoft.AnalysisServices.Tabular.DataColumn()
                        {
                            Name = "AltId",
                            DataType = DataType.Int64,
                            SourceColumn = "ProductSubcategoryAlternateKey",
                        },
                        new Microsoft.AnalysisServices.Tabular.DataColumn()
                        {
                            Name = "EnglishSubCategory",
                            DataType = DataType.String,
                            SourceColumn = "EnglishProductSubcategoryName",
                        }
                    }
                });
                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);
         }

0 个答案:

没有答案