|
2008-3-10
通过C#程序配置报表到SQL Server 2005报表服务(SSRS)
介绍 大多数情况下,用户通过Visual Studio 报表管理器手动配置报表是可以接收的。不过有些时候,有可能会有一些硬性规定,迫使用户不得不通过MSI配置任何可配置的报表(如win Form程序,WebForm程序,SSRS报表)。 这个例子要求一个配置文件,以收集信息,如报表的数据源应创建到什么地方,创建为什么名字,数据源所用的用户名和密码。使得这些信息动态可变是很重要的,因为当一个报表被配置到产品发布服务器上的时候,经常需要更换它的数据源,而不是用以前开发时用的那个。
代码的使用 一旦安装的信息从app.config文件中得到,Install方法就会开始配置报表到服务器上。
必须注意的步骤: 1,添加SQL Reporting Service 2005的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
{
// 存储 database provider 类型.
private string _extension = string.Empty;
// 报表数据来源的服务器名字
private string _serverName = string.Empty;
// Variable to hold the DataSourceLocation in the report server for
// the deployed report.
private string _datasourceLocation = string.Empty;
// 报表发布到的服务器上的报表目录
private string _parent = string.Empty;
// Variable to hold the folder name in the report
// server to deploy the report.
private string _folder = string.Empty;
// 每个报表的ConnectionString连接字符串
private string _connectString = string.Empty;
// 访问报表服务器的webservice 客户端
private ReportingService2005.ReportingService2005 _rs
= new DeployReport.ReportingService2005.ReportingService2005();
// 报表取数据的数据库
private string _dbName = string.Empty;
// 报表所用的数据源名字
private string _datasource = string.Empty;
// 报表配置到的全路径名
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();
}
// 配置报表到服务器
private void Deploy()
{
foreach (object _report in this.ConnectionStrings)
{
// 呵呵连接字符串
//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();
// 打开报表文件,并写入内存
byte[] _reportDefinition;
Warning[] _warnings;
FileStream _stream = File.OpenRead( this._reportPath);
_reportDefinition = new Byte[_stream.Length];
_stream.Read( _reportDefinition, 0, (int)_stream.Length);
_stream.Close();
// 向服务器创建报表
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);
// 创建数据源
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,
// 修改数据源引用
// 保证报表指向正确的数据集
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
/// This is the section name in the config
/// file
/// This is the name of the key in the specfied
/// section in the config file
/// string
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;
}
/// 检查目录是否存在
/// Parent folder path
/// Name of the folder to search
/// True if found, else false.
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;
}
/// 设置报表定义的信息,如
/// 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;
}
}
}
}
热门文章
推荐信息
|