如何使用AMO表格库将结构化ODBC数据源添加到表格模型

时间:2019-06-23 08:56:10

标签: c# ssas-tabular analysisservices

向我的模型添加结构化的ODBC数据源会导致错误。

我想使用结构化(即M / Power Query),非传统(即ProviderDataSource)数据源的Microsoft.AnalysisServices.Tabular库在兼容级别为1400的SQL Analysis Services服务器上生成表格模型。 我使用NuGet包Microsoft.AnalysisServices.retail.amd64(16.3.0)安装了该库。

这是我的数据源定义。

                myDatabase.Model.DataSources.Add(new StructuredDataSource()
                {
                    Name = "ODBC",
                    Description = "An structured ODBC data source definition",
                    ConnectionDetails = new ConnectionDetails()
                    {
                        Protocol = DataSourceProtocol.Odbc
                    },
                    Credential = new Credential()
                    {
                        AuthenticationKind = AuthenticationKind.UsernamePassword,
                        EncryptConnection = false,
                        Username = "MYUSERNAME",
                        Password = "MYPASSWORD"
                    }
                }

运行此代码时,我得到:

COM error: Microsoft.Data.Mashup; The given data source reference is not a valid data source.

它没有给我任何指向哪里可以查看或具体出了什么问题。我怀疑定义需要服务器地址,但是无法根据文档设置ConnectionDetails对象的address属性。

ConnectionDetails.Address Property

Address of this connection. It can't be set, instead it should be modified directly.

1 个答案:

答案 0 :(得分:2)

在回顾我的问题并偏离路线进一步调查文档时,我构建了此解决方案。对于那些遇到同样问题的人,我认为将其发布在这里是很好的选择。

Credential credential = new Credential()
{
    AuthenticationKind = AuthenticationKind.UsernamePassword,
    EncryptConnection = false,
    Username = "MYUSERNAME",
    Password = "MYPASSWORD" // Please note that this won't persist.
};

ConnectionDetails connectionDetails = new ConnectionDetails("{ 'protocol': 'odbc', 'address': { 'options': { 'dsn': 'MYODBCDSN' } }, 'authentication': null, 'query': null }");

dbWithDataSource.Model.DataSources.Add(new StructuredDataSource()
{
    Name = "ODBC",
    Description = "An ODBC structured (ie. non-legacy) data source definition",
    ConnectionDetails = connectionDetails,
    Credential = credential,
    Options = new DataSourceOptions( "{ 'hierarchicalNavigation': true }" )
}

我基本上所做的就是在ConnectionDetails构造函数中传递JSON字符串,同时设置“只读”地址属性。