分析データ ソースに対するコマンドの実行

分析データ ソースへの接続を確立した後、AdomdCommand オブジェクトを使用して、そのデータ ソースに対してコマンドを実行し、結果を取得することができます。 これらのコマンドによるデータ取得には、多次元式 (MDX)、データ マイニング拡張機能 (DMX)、さらには SQL の一部の構文も使用できます。 また、Analysis Services Scripting Language (ASSL) コマンドを使用して、基になるデータベースを変更することもできます。

コマンドの作成

コマンドを実行するには、まず、そのコマンドを作成する必要があります。 コマンドを作成するには 2 つの方法があります。

  • 1 つは、AdomdCommand コンストラクターを使用する方法です。この場合、データ ソースに対して実行するコマンドと、そのコマンドの実行対象となる AdomdConnection オブジェクトを指定します。

  • もう 1 つは、AdomdConnection オブジェクトの CreateCommand メソッドを使用する方法です。

実行するコマンドのテキストは、CommandText プロパティを使用して照会および変更できます。 作成するコマンドは、実行後に必ずしもデータを返す必要はありません。

コマンドの実行

AdomdCommand オブジェクトを作成したら、Execute メソッドを使用して、コマンドからさまざまな操作を実行することができます。 次の表は、実行可能な操作の一部を示しています。

目的

使用するメソッド

結果をデータのストリームとして返す

ExecuteReaderAdomdDataReader オブジェクトを返す

CellSet オブジェクトを返す

ExecuteCellSet

行を返さないコマンドを実行する

ExecuteNonQuery

XML for Analysis (XMLA) に準拠した形式のデータを含む XMLReader オブジェクトを返す

ExecuteXmlReader

コマンドの実行例

この例では、AdomdCommand を使用して XMLA コマンドを実行します。このコマンドは、ローカル サーバー上の Adventure Works DW キューブを処理し、データを返しません。

        void ExecuteXMLAProcessCommand()
        {
            //Open a connection to the local server
            AdomdConnection conn = new AdomdConnection("Data Source=localhost");
            conn.Open();

            //Create a command, and assign it an XMLA command to process the cube.
            AdomdCommand cmd = conn.CreateCommand();
            cmd.CommandText = "<Process xmlns=\"https://schemas.microsoft.com/analysisservices/2003/engine\">\r\n" +
  @"<Object>
    <DatabaseID>Adventure Works DW Standard Edition</DatabaseID>
    <CubeID>Adventure Works DW</CubeID>
  </Object>
  <Type>ProcessFull</Type>
  <WriteBackTableCreation>UseExisting</WriteBackTableCreation>
</Process>";

            //Execute the command
            int result = cmd.ExecuteNonQuery();

            //Close the connection
            conn.Close();
        }