How to test connection to a data source in SSAS using C#

元气小坏坏 提交于 2019-12-05 16:18:47

The best solution I have come across to doing this connectivity test is below: Please note that this requires the Microsoft.AnalysisServices.DLL to be added as a reference.

class ConnectivityTests
{
    // Variables
    String serverName = "";
    String databaseName = "";
    String dataSourceName = "";

    [Test]
    public void TestDataSourceConnection()
    {
        try
        {

            // Creates an instance of the Server
            Server server = new Server();
            server.Connect(serverName);

            // Gets the Database from the Server
            Database database = server.Databases[databaseName];

            // Get the DataSource from the Database
            DataSource dataSource = database.DataSources.FindByName(dataSourceName);

            // Attempt to open a connection to the dataSource.  Fail test if unsuccessful
            OleDbConnection connection = new OleDbConnection(dataSource.ConnectionString);

            connection.Open();
        }
        catch (Exception e)
        {
            Assert.Fail(e.ToString());
        }
        finally
        {
            connection.Close();
        }

     }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!