以编程方式部署 SQL Reporting Services 2005 报表






4.25/5 (9投票s)
2006年6月9日

206852

1766
本文档介绍了如何使用 SQL Reporting Services 2005 暴露的 Web 服务,以编程方式将 SQL 报告部署到 Reporting Server 2005。
引言
大多数情况下,客户可以通过 Visual Studio 报告管理器手动部署报告,这是可以接受的。但有时,可能存在一项硬性规定,即“任何可部署的内容,例如 WinForms 应用程序、WebForms 应用程序或 SSRS 报告等,都必须通过 MSI 部署。”
此示例基本上有一个config文件,用于收集信息,例如应创建报告的数据源的位置,以及其名称,以及应在数据源中使用的用户名和密码。使所有这些信息动态化非常重要,因为通常会发布一个报告,而其数据源指向与最初创建它的机器上的生产服务器上的不同位置。
使用代码
一旦从app.config收集了设置信息,Install
方法就开始将报告部署到服务器上。
源代码
需要注意的步骤
- 为 SSRS(SQL Reporting Service 2005)暴露的 Web 服务添加 Web 引用。
- 设置配置设置[例如报告名称、数据库连接等]。
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using DeployReport.ReportingService2005;
namespace DeployReport
{
public class ReportInstaller
{
// Variable to hold the database provider type.
private string _extension = string.Empty;
// Variable to hold the Servername to fetch the data for the report.
private string _serverName = string.Empty;
// Variable to hold the DataSourceLocation in the report server for
// the deployed report.
private string _datasourceLocation = string.Empty;
// Variable to hold the parent folder name in the report server to
// deploy the report.
private string _parent = string.Empty;
// Variable to hold the folder name in the report
// server to deploy the report.
private string _folder = string.Empty;
// Variable to hold the ConnectionString for each report.
private string _connectString = string.Empty;
// Variable to hold the webservice client to access the reporting server.
private ReportingService2005.ReportingService2005 _rs
= new DeployReport.ReportingService2005.ReportingService2005();
// Variable to hold the database name to fetch the data for the report.
private string _dbName = string.Empty;
// Variable to hold the datasource name used by the report.
private string _datasource = string.Empty;
// Variable to hold the full path of the report to be deployed.
private string _reportPath = string.Empty;
// Variable to hold the properties of finditems.
private CatalogItem[] _returnedItems;
// Collection to hold the reports to deploy.
private ArrayList ConnectionStrings = new ArrayList();
public ReportInstaller()
{
// This call is required by the Designer.
InitializeComponent();
this._extension = this.GetValueFromConfig("appSettings", "Extension");
this._serverName = this.GetValueFromConfig("appSettings", "ServerName");
this._datasourceLocation = this.GetValueFromConfig("appSettings",
"DataSourceLocation");
this._parent = this.GetValueFromConfig("appSettings", "Parent");
this._folder = this.GetValueFromConfig("appSettings", "Folder");
// Set security credentials for web service client authorization.
_rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Create report folder to drop report in.
if ( this._folder.Length > 0 && this._parent.Length > 0)
{
// Create folder if not exists.
if (this.CheckExist(ItemTypeEnum.Folder,
this._parent, this._folder) == false)
{
_rs.CreateFolder( this._folder, this._parent, null);
}
}
else
{
// Log the error.
}
this.GetReportsToDeploy( "ReportDetails");
this.Deploy();
}
// Deploys the report to the reporting server.
private void Deploy()
{
foreach (object _report in this.ConnectionStrings)
{
// Get the connection string for the report to deploy.
//this._connectString =
// this.GetValueFromConfig( "ReportDetails",
// "Report1");
this._connectString = _report.ToString();
// Continue deploying report if report details provided.
if ( this._connectString.Length > 0)
{
// Set report details.
this.SetReportInfo();
// Open the report (rdl) file and
// read the data into the stream.
byte[] _reportDefinition;
Warning[] _warnings;
FileStream _stream = File.OpenRead( this._reportPath);
_reportDefinition = new Byte[_stream.Length];
_stream.Read( _reportDefinition, 0, (int)_stream.Length);
_stream.Close();
// Create the report into the server.
string _reportName = this._reportPath.Substring(
this._reportPath.LastIndexOf("\\") + 1);
_warnings = (Warning[])_rs.CreateReport( _reportName.Remove
( _reportName.Length - 4, 4),
this._parent + this._folder, true,
_reportDefinition, null);
// Create datasource for the report.
DataSource dSource = new DataSource();
DataSourceDefinition dDefinition =
new DataSourceDefinition();
dSource.Item = dDefinition;
dDefinition.Extension = this._extension;
dDefinition.ConnectString = @"Data Source=" + this._serverName
+ @";Initial Catalog=" + this._dbName;
dDefinition.ImpersonateUserSpecified = true;
dDefinition.Prompt = null;
dDefinition.WindowsCredentials = true;
dDefinition.CredentialRetrieval =
CredentialRetrievalEnum.Integrated;
dSource.Name = this._datasource;
try
{
if (this.CheckExist( ItemTypeEnum.DataSource, this._parent,
this._datasourceLocation + "/" +
this._datasource) == false)
{
_rs.CreateDataSource( this._datasource, @"/" +
this._datasourceLocation, false, dDefinition, null);
}
}
catch (System.Web.Services.Protocols.SoapException _exception)
{
throw _exception;
}
// Report and Datasource created,
// now fix up datasource reference to
// make sure report points at correct dataset.
try
{
DataSourceReference reference = new DataSourceReference();
DataSource ds = new DataSource();
reference.Reference = @"/" + this._datasourceLocation + @"/"
+ this._datasource;
DataSource[] dsarray = _rs.GetItemDataSources( this._parent +
this._folder + "/" + _reportName.Remove
( _reportName.Length - 4, 4));
ds = dsarray[0];
ds.Item = (DataSourceReference)reference;
_rs.SetItemDataSources( this._parent +
this._folder + "/" + _reportName.Remove
( _reportName.Length - 4, 4), dsarray);
}
catch (Exception _exception)
{
throw (_exception);
}
}
}
}
/// This function reads a setting from the config file.Empty string
/// returned if no setting was found. Raise error if config can not be
/// read
/// <param name="sectionName">This is the section name in the config
/// file</param>
/// <param name="keyName">This is the name of the key in the specfied
/// section in the config file</param>
/// <returns>string</returns>
private string GetValueFromConfig(string sectionName, string keyName)
{
string _value = string.Empty;
try
{
// Read the configuration settings from the configuration file
NameValueCollection _section = (NameValueCollection)
ConfigurationSettings.GetConfig( sectionName);
// If there is a key return , else return empty string
if (_section != null && _section[keyName] != null)
{
_value = _section[keyName];
}
}
catch (Exception _exception)
{
// Consume the error, so that the service keeps running,
// but log the error.
EventLog.WriteEntry( Assembly.GetExecutingAssembly().FullName,
_exception.Message, EventLogEntryType.Error);
}
return _value;
}
/// Checks if the folder exists or not.
/// <param name="path">Parent folder path</param>
/// <param name="folderName">Name of the folder to search</param>
/// <returns>True if found, else false.</returns>
private bool CheckExist(ItemTypeEnum type, string path, string folderName)
{
string _path = path + folderName;
// Condition criteria.
SearchCondition[] conditions;
// Condition criteria.
SearchCondition condition = new SearchCondition();
condition.Condition = ConditionEnum.Contains;
condition.ConditionSpecified = true;
condition.Name = "Name";
condition.Value = "";
conditions = new SearchCondition[1];
conditions[0] = condition;
this._returnedItems = _rs.FindItems( path,
BooleanOperatorEnum.Or, conditions);
// Iterate thro' each report properties to get the path.
foreach (CatalogItem item in _returnedItems)
{
if (item.Type == type)
{
if (item.Path == _path)
return true;
}
}
return false;
}
/// Sets report definition such
/// as databasename to connect, datasource
/// of the report and path for the report.
private void SetReportInfo()
{
string[] _temp = this._connectString.Split( ";".ToCharArray());
string[] _properties = new string[_temp.Length];
for (int _count = 0; _count < _temp.Length; _count++)
{
_properties[_count] =
_temp[_count].Split("=".ToCharArray())[1];
}
// Get the deployment info for the report.
if (_properties.Length == 3)
{
this._dbName = _properties[0];
this._datasource = _properties[1];
this._reportPath = _properties[2];
}
}
/// Gets all the tags under the section.
private void GetReportsToDeploy(string sectionName)
{
try
{
// Read the configuration settings from the configuration file
NameValueCollection _section = (NameValueCollection)
ConfigurationSettings.GetConfig( sectionName);
this.ConnectionStrings.Clear();
if (_section != null && _section.HasKeys() == true)
{
for (int _count = 0; _count < _section.Keys.Count; _count++)
{
this.ConnectionStrings.Add( _section[_section.Keys[_count]]);
}
}
}
catch (Exception _exception)
{
throw _exception;
}
}
}
}
就是这样!