【问题标题】:Sql reporting Services - Programmatically setting a datasource?Sql 报告服务 - 以编程方式设置数据源?
【发布时间】:2009-10-21 16:28:33
【问题描述】:
我有一堆报告作为 RDL 部署到 SSRS。由于安全性要求高,数据库密码更改非常频繁。跟上变化并不得不修改数十个报告已成为一项艰巨的任务。这就引出了我的问题……
是否可以以编程方式为已部署的报表设置数据源或连接字符串?
是否可以使用一个应用程序来修改报告本身中的某些内容,因为它位于服务器上?
当 DS 位于服务器上时,这可以通过从应用修改共享数据源来完成吗?
是否可以通过在报告本身中嵌入一个脚本来完成,该脚本从 Web 服务中检索连接?
谢谢
【问题讨论】:
标签:
c#
reporting-services
reportingservices-2005
【解决方案1】:
这可以通过多种方式完成,我认为最简单的一种是使用 SSRS Web 服务的 API。 Web 服务允许您操作所有报告实体,包括 Data Sources。
作为此问题的解决方案,可以在每次 DB 密码更改时调用 Web 服务来更新数据源凭据(甚至使用某种触发器自动执行?)。
我在一个项目中做了类似的事情,其中 RDL 和数据源必须在运行时生成、部署和操作,并且工作正常,因此更新数据源必须是可行的。
【解决方案2】:
为您的报告服务端点 (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);
}
}
}