-->

如何使用C#来测试SSAS连接到数据源(How to test connection to a da

2019-09-22 21:56发布

我有一个远程服务器上的Analysis Services数据库。 这包含位于另一台远程服务器上的另一个数据库的数据源。

我试图写一个连通性测试,使用C#这将检查两个数据库之间的数据库连接。

我一直无法做到这一点使用ADOMD.NET。 目前我正在考虑使用SMO这样做,但我没有任何运气至今。

我将不胜感激任何意见或建议。

更新:
经过进一步研究,我想出了下面的测试(请注意,我打算以后添加更多在try..catch块,并断言)。

此外,该使用C:\ Program Files文件\ Microsoft SQL Server的\ 100 \ SDK \组件\ Microsoft.AnalysisServices.DLL访问服务器,数据库和数据源类。

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

    [Test]
    public void TestDataSourceConnection()
    {
        // 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);
        try
        {
            connection.Open();
        }
        catch (OleDbException e)
        {
            Assert.Fail(e.ToString());
        }
        finally
        {
            connection.Close();
        }

    }

我认为,这个测试就足够了我的测试(一旦我增加了一些在try..catch块,并断言)。 如果测试通过,就意味着有我的机器和两台服务器,这意味着不应该有这些服务器之间的连接问题之间没有任何连接问题。

不过,我一直无法工作,如何直接测试两个服务器之间的连接,我有兴趣,如果任何人都知道这样做的方式。

Answer 1:

我所遇到到这样的连通性测试最好的解决办法是如下所示:请注意,这需要添加作为参考Microsoft.AnalysisServices.DLL。

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();
        }

     }
}


文章来源: How to test connection to a data source in SSAS using C#