Sql报告服务 - 以编程方式设置数据源?

时间:2009-10-21 16:28:33

标签: c# reporting-services reportingservices-2005

我有一堆报告作为RDL部署到SSRS。由于高安全性要求,db密码会经常更改。要跟上变化并且不得不修改几十个报告,这是一项艰巨的任务。这引出了我的问题......

是否可以以编程方式为已部署的报告设置数据源或连接字符串?

  • 这是否可以使用修改报表本身内容的应用程序来完成,因为它位于服务器上?

  • 这可以通过在DS位于服务器上的应用修改共享数据源来完成吗?

  • 是否可以通过在报表本身中嵌入一个脚本来完成,该脚本从Web服务中检索连接?

由于

2 个答案:

答案 0 :(得分:4)

这可以通过多种方式完成,我认为最简单的方法之一是使用SSRS Web服务的API。通过Web服务,您可以操作所有报告实体,包括Data Sources

作为此问题的解决方案,每次数据库密码更改时,都可以使用对Web服务的调用来更新数据源凭据(甚至可以使用某种触发器自动执行此操作?)。

我在一个项目中做了类似的事情,其中​​RDL和数据源必须在运行时生成,部署和操作,并且工作正常,因此更新数据源必须是可行的。

答案 1 :(得分:0)

为您的Reporting Service端点(http://ReportServerHost.mydomain.tld/ReportServer/ReportService2005.asmx)添加服务引用。使用以下代码修改数据源密码:

    public static void ChangeDataSourcePassword(string dataSourcePath, string password)
    {
        using (ReportingService2005SoapClient reportingService = new ReportingService2005SoapClient())
        {
            reportingService.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;

            try
            {  
                ServerInfoHeader serverInfo = null;
                DataSourceDefinition dataSourceDefinition = null;

                serverInfo = reportingService.GetDataSourceContents(dataSourcePath, out dataSourceDefinition);
                dataSourceDefinition.Password = password;
                serverInfo = reportingService.SetDataSourceContents(null, dataSourcePath, dataSourceDefinition);
            }
            catch (FaultException ex)
            {
                // Do something with the exception. Rethrow it and/or show it to the user.
                Console.WriteLine(string.Format("Failed to change the password on {0}: {1}", dataSourcePath, ex.Message));
                throw new ApplicationException("Failed to change the password on the Data Source.", ex);
            }
        }
    }