データ ソースを表形式モデルに追加する (AMO-TOM)
適用対象:
SQL Server 2016 以降のAnalysis Services
Azure Analysis Services
Power BI Premium
Microsoft.AnalysisServices.Tabular 名前空間の DataSource クラスは、データ更新操作中にインポートされたデータの種類と場所を指定する表形式モデルのデータ ソースの抽象化です。
DataSource から派生したクラスのオブジェクトをインスタンス化し、 それを Model オブジェクトの DataSources コレクションに追加することで、データ ソースを表形式モデルに追加できます。 サーバーに変更をコミットするには、 Model.SaveChanges() または Database.Update(UpdateOptions.ExpandFull) を呼び出します。
表形式モデルでは、リレーショナル データ ソースからのデータインポートのみをサポートします。データ プロバイダーは、テーブルと列の形式でデータを公開します。 そのため、表形式オブジェクト モデルでは、ProviderDataSource クラス (DataSource から派生) を使用してこの機能が公開されます。
コード例: データ ソースを追加する
この例では、 ProviderDataSource をインスタンス化してデータ モデルに追加する方法を示します。
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 Data Source Definition");
//
// Instantiate a new
// Microsoft.AnalysisServices.Tabular.Database object.
//
var dbWithDataSource = 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.
//
dbWithDataSource.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.
//
dbWithDataSource.Model.DataSources.Add(new ProviderDataSource()
{
Name = "SQL Server Data Source Example",
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 the new database object to the server's
// Databases connection and submit the changes
// with full expansion to the server.
//
server.Databases.Add(dbWithDataSource);
dbWithDataSource.Update(UpdateOptions.ExpandFull);
Console.Write("Database ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write(dbWithDataSource.Name);
Console.ResetColor();
Console.WriteLine(" created successfully.");
Console.WriteLine("The data model includes the following data source definitions:");
Console.ForegroundColor = ConsoleColor.Yellow;
foreach (DataSource ds in dbWithDataSource.Model.DataSources)
{
Console.WriteLine("\tData source name:\t\t{0}", ds.Name);
Console.WriteLine("\tData source description:\t{0}", ds.Description);
}
Console.ResetColor();
Console.WriteLine();
}
Console.WriteLine("Press Enter to close this console window.");
Console.ReadLine();
}
}
}
次のステップ
モデル内では、データ ソース内のソース列をモデル内の変換先列にマップするデータ バインディングを設定する必要があります。 また、データを格納するパーティション (テーブルごとに少なくとも 1 つ) を定義する必要があります。 次のリンクでは、テーブル、パーティション、 および列を作成する方法を示します。