首页 > 解决方案 > 如何将带有代码的 csv 文件导入表格模型

问题描述

我正在设置一个网络服务,用户上传 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);
         }

标签: c#ssas-tabularimport-csv

解决方案


推荐阅读