共用方式為


在表格式模型中建立資料表、資料分割和資料行

適用于:SQL Server 2016 和更新版本的 Analysis Services Azure Analysis Services Fabric/Power BI Premium

在表格式模型中,資料表是由資料列和資料行所組成。 資料列會組織成資料分割以支援累加式資料重新整理。 表格式解決方案可以支援數種類型的資料表,視資料的來源而定:

  • 一般資料表,其中資料是透過資料提供者源自關聯式資料來源。

  • 推送的資料表,其中資料會以程式設計方式「推送」至資料表。

  • 匯出資料表,其中資料來自 DAX 運算式,參考模型內另一個物件的資料。

在下列程式碼範例中,我們將定義一般資料表。

必要的元素

資料表必須至少有一個資料分割。 一般資料表也必須至少定義一個資料行。

每個分割區都必須有指定資料來源的 Source,但 source 可以設定為 null。 一般而言,來源是查詢運算式,可定義相關資料庫查詢語言中的資料配量。

程式碼範例:建立資料表、資料行、資料分割

資料表是由 Microsoft.AnalysisServices.Tabular 命名空間) 中的 Table 類別 (來表示。

在下列範例中,我們將定義一般資料表,其中一個分割區連結至關聯式資料來源和一些一般資料行。 我們也會將變更提交至伺服器,並觸發將資料帶入模型的資料重新整理。 當您想要將資料從SQL Server關係資料庫載入表格式解決方案時,這代表最常見的案例。

using System; 
using Microsoft.AnalysisServices; 
using Microsoft.AnalysisServices.Tabular; 
 
namespace TOMSamples 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            // 
            // Connect to the local default instance of Analysis Services 
            // 
            string ConnectionString = "DataSource=localhost"; 
 
            // 
            // The using syntax ensures the correct use of the 
            // Microsoft.AnalysisServices.Tabular.Server object. 
            // 
            using (Server server = new Server()) 
            { 
                server.Connect(ConnectionString); 
 
                // 
                // Generate a new database name and use GetNewName 
                // to ensure the database name is unique. 
                // 
                string newDatabaseName = 
                    server.Databases.GetNewName("Database with a Table Definition"); 
 
                // 
                // Instantiate a new  
                // Microsoft.AnalysisServices.Tabular.Database object. 
                // 
                var dbWithTable = new Database() 
                { 
                    Name = newDatabaseName, 
                    ID = newDatabaseName, 
                    CompatibilityLevel = 1200, 
                    StorageEngineUsed = StorageEngineUsed.TabularMetadata, 
                }; 
 
                // 
                // Add a Microsoft.AnalysisServices.Tabular.Model object to the 
                // database, which acts as a root for all other Tabular metadata objects. 
                // 
                dbWithTable.Model = new Model() 
                { 
                    Name = "Tabular Data Model", 
                    Description = "A Tabular data model at the 1200 compatibility level." 
                }; 
 
                // 
                // Add a Microsoft.AnalysisServices.Tabular.ProviderDataSource object 
                // to the data Model object created in the previous step. The connection 
                // string of the data source object in this example  
                // points to an instance of the AdventureWorks2014 SQL Server database. 
                // 
                string dataSourceName = "SQL Server Data Source Example"; 
                dbWithTable.Model.DataSources.Add(new ProviderDataSource() 
                { 
                    Name = dataSourceName, 
                    Description = "A data source definition that uses explicit Windows credentials for authentication against SQL Server.", 
                    ConnectionString = "Provider=SQLNCLI11;Data Source=localhost;Initial Catalog=AdventureWorks2014;Integrated Security=SSPI;Persist Security Info=false", 
                    ImpersonationMode = Microsoft.AnalysisServices.Tabular.ImpersonationMode.ImpersonateAccount, 
                    Account = @".\Administrator", 
                    Password = "P@ssw0rd", 
                }); 
 
                //  
                // Add a table called Individual Customers to the data model 
                // with a partition that points to a [Sales].[vIndividualCustomer] view 
                // in the underlying data source. 
                // 
                dbWithTable.Model.Tables.Add(new Table() 
                { 
                    Name = dbWithTable.Model.Tables.GetNewName("Individual Customers"), 
                    Description = "Individual customers (names and email addresses) that purchase Adventure Works Cycles products online.", 
                    Partitions = { 
                        // 
                        // Create a single partition with a QueryPartitionSource for a query 
                        // that imports all customer rows from the underlying data source. 
                        // 
                        new Partition() { 
                            Name = "All Customers", 
                            Source = new QueryPartitionSource() { 
                                DataSource = dbWithTable.Model.DataSources[dataSourceName], 
                                Query = @"SELECT   [FirstName] 
                                                    ,[MiddleName] 
                                                    ,[LastName] 
                                                    ,[PhoneNumber]  
                                                    ,[EmailAddress] 
                                                    ,[City] 
                                        FROM [Sales].[vIndividualCustomer]", 
                            } 
                        } 
                    }, 
                    Columns = 
                    { 
                        // 
                       // DataColumn objects refer to regular table columns.  
                        // Each DataColumn object corresponds to a column in the underlying data source query. 
                        // 
                        new DataColumn() { 
                            Name = "FirstName", 
                            DataType = DataType.String, 
                            SourceColumn = "FirstName", 
                        }, 
                        new DataColumn() { 
                            Name = "MiddleName", 
                            DataType = DataType.String, 
                            SourceColumn = "MiddleName", 
                        }, 
                        new DataColumn() { 
                            Name = "LastName", 
                            DataType = DataType.String, 
                            SourceColumn = "LastName", 
                        }, 
                        new DataColumn() { 
                            Name = "PhoneNumber", 
                            DataType = DataType.String, 
                            SourceColumn = "PhoneNumber", 
                        }, 
                        new DataColumn() { 
                            Name = "EmailAddress", 
                            DataType = DataType.String, 
                            SourceColumn = "EmailAddress", 
                        }, 
                        new DataColumn() { 
                            Name = "City", 
                            DataType = DataType.String, 
                            SourceColumn = "City", 
                        }, 
                    } 
                }); 
 
                // 
                // Add the new database object to the server's  
                // Databases connection and submit the changes 
                // with full expansion to the server. 
                // 
                server.Databases.Add(dbWithTable); 
 
                //  
                // Request a full refresh to import the data from the data source and 
                // and perform any necessary recalculations. 
                // The refresh operation will be performed with the next 
                // invocation of Model.SaveChanges() or Database.Update(UpdateOptions.ExpandFull). 
                dbWithTable.Model.RequestRefresh(Microsoft.AnalysisServices.Tabular.RefreshType.Full); 
                dbWithTable.Update(UpdateOptions.ExpandFull); 
 
 
                Console.Write("Database "); 
                Console.ForegroundColor = ConsoleColor.Yellow; 
                Console.Write(dbWithTable.Name); 
                Console.ResetColor(); 
                Console.WriteLine(" created successfully."); 
 
                Console.WriteLine("The data model includes the following table definitions:"); 
                Console.ForegroundColor = ConsoleColor.Yellow; 
                foreach (Table tbl in dbWithTable.Model.Tables) 
                { 
                    Console.WriteLine("\tTable name:\t\t{0}", tbl.Name); 
                    Console.WriteLine("\ttbl description:\t{0}", tbl.Description); 
                } 
                Console.ResetColor(); 
                Console.WriteLine(); 
            } 
            Console.WriteLine("Press Enter to close this console window."); 
            Console.ReadLine(); 
        } 
    } 
} 

資料表中的資料分割

資料分割是由 Microsoft.AnalysisServices.Tabular 命名空間) 中的 Partition 類別 (來表示。 Partition類別會公開 PartitionSource類型的Source屬性,以提供將資料擷取到資料分割的不同方法的抽象概念。 資料分割實例的Source屬性可以是 Null,表示資料會藉由將資料區區塊轉送至伺服器,作為 Analysis Services 所公開之推送資料 API 的一部分,將資料推送至資料分割。 在 SQL Server 2016 中,PartitionSource類別有兩個衍生類別,代表將資料系結至資料分割的方式:QueryPartitionSourceCalculatedPartitionSource

資料表中的資料行

資料行是由衍生自 Microsoft.AnalysisServices.Tabular 命名空間中基底 Column 類別 (的數個類別來表示) :

  • 一般資料表中一般資料行的 DataColumn ()
  • DAX 運算式所支援之資料行的CalculatedColumn ()
  • 匯出資料表中一般資料行的CalculatedTableColumn ()
  • RowNumberColumn (SSAS 針對每個資料表所建立的特殊資料行類型) 。

資料表中的資料列編號

伺服器上的每個 Table 物件都有用於編制索引的 RowNumberColumn 。 您無法明確建立或新增它。 當您儲存或更新物件時,會自動建立資料行:

  • Db。SaveChanges

  • Db。更新 (ExpandFull)

呼叫任一方法時,伺服器會自動建立資料列編號資料行,這會顯示為 RowNumberColumn 資料表的 Columns 集合。

計算資料表

匯出資料表的來源是 DAX 運算式,可從模型中現有資料結構或從內嵌系結重新用途的資料。 若要以程式設計方式建立匯出資料表,請執行下列動作:

  • 建立泛型 資料表

  • 使用 CalculatedPartitionSource類型的 Source 將分割區新增至其中,其中來源是 DAX 運算式。 分割區的來源是一般資料表與匯出資料表的不同之處。

當您將變更儲存至伺服器時,伺服器會傳回 CalculatedTableColumns 的推斷清單, (計算資料表是由計算資料表資料行所組成,) ,可透過資料表的 Columns 集合顯示。

後續步驟

檢閱用於處理 TOM 中例外狀況的類別: 處理 TOM 中的錯誤