Вот код, который я использую в проекте для просмотра отчетов Crystal (это веб-элемент управления)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Objects.Controls;
using System.Data.SqlClient;
using Objects.Database;
using System.IO;
using System.Configuration;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
namespace App.WebControls
{
/// <summary>
/// Crystal Report Viewer Control
/// </summary>
public partial class CrystalReportViewer : BaseWebControl
{
//- MEMBER VARIABLES --------------------------------------------------------------------------------------------------------
#region Members
private CrystalDecisions.Shared.ParameterFields m_ParameterFields = new CrystalDecisions.Shared.ParameterFields();
private CrystalDecisions.Web.CrystalReportSource m_CrystalReportSource = new CrystalDecisions.Web.CrystalReportSource();
#endregion Members
//- PROPERTIES --------------------------------------------------------------------------------------------------------------
#region Properties
/// <summary>
/// Gets or Sets the Crystal Report Source
/// </summary>
public CrystalDecisions.Web.CrystalReportSource ReportSource
{
get { return m_CrystalReportSource; }
set { m_CrystalReportSource = value; }
}
/// <summary>
/// Gets the Name of the WAP data source name
/// </summary>
public string WAPDataSourceName
{
get
{
if ( ViewState[ "WAPDataSourceName" ] == null )
{
ViewState[ "WAPDataSourceName" ] = "WAP";
}
return ViewState[ "WAPDataSourceName" ] as string;
}
set
{
ViewState[ "WAPDataSourceName" ] = value;
}
}
/// <summary>
/// Gets the Name of the Sage Datasource Name
/// </summary>
public string SageDataSourceName
{
get
{
if ( ViewState[ "SageDataSourceName" ] == null )
{
ViewState[ "SageDataSourceName" ] = "WAP_Sage";
}
return ViewState[ "SageDataSourceName" ] as string;
}
set
{
ViewState[ "SageDataSourceName" ] = value;
}
}
/// <summary>
/// Gets or Sets the Report Filename
/// </summary>
public string ReportFileName
{
get
{
return ReportSource.Report.FileName;
}
set
{
ReportSource.Report.FileName = value;
}
}
/// <summary>
/// Gets or Sets the Sage Database
/// </summary>
public SageDatabase SageDatabase
{
get
{
if ( ViewState[ "SageDatabase" ] == null )
{
ViewState[ "SageDatabase" ] = new SageDatabase();
}
return ViewState[ "SageDatabase" ] as SageDatabase;
}
set
{
ViewState[ "SageDatabase" ] = value;
}
}
/// <summary>
/// Gets the Current Paramter Fields
/// </summary>
public CrystalDecisions.Shared.ParameterFields ParameterFields
{
get
{
return m_ParameterFields;
}
}
#endregion Properties
//- EVENTS ------------------------------------------------------------------------------------------------------------------
#region Events
/// <summary>
/// Page Load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load( object sender, EventArgs e )
{
try
{
if ( !this.IsPostBack )
{
//Set up the Report Source
this.SetReportSource();
//Set the Connections
this.ConfigureReports();
//Sets the Parameters
this.SetParameters();
}
}
catch ( Exception )
{
throw;
}
}
#endregion Events
//- METHODS -----------------------------------------------------------------------------------------------------------------
#region Methods
/// <summary>
/// Gets a connection info object based on a SQL connection string
/// </summary>
/// <param name="oSqlConnectionStringBuilder">The connection string builder</param>
/// <param name="ServerName">The server name the connection is for</param>
/// <returns>Connection Info</returns>
private ConnectionInfo GetConnectionInfo( SqlConnectionStringBuilder oSqlConnectionStringBuilder, string ServerName )
{
try
{
ConnectionInfo oConnectionInfo = new ConnectionInfo();
oConnectionInfo.ServerName = ServerName;
oConnectionInfo.DatabaseName = oSqlConnectionStringBuilder.InitialCatalog;
oConnectionInfo.UserID = oSqlConnectionStringBuilder.UserID;
oConnectionInfo.Password = oSqlConnectionStringBuilder.Password;
oConnectionInfo.IntegratedSecurity = oSqlConnectionStringBuilder.IntegratedSecurity;
oConnectionInfo.Type = ConnectionInfoType.SQL;
oConnectionInfo.AllowCustomConnection = true;
return oConnectionInfo;
}
catch
{
throw;
}
}
/// <summary>
/// Sets the DB logon info for the report
/// </summary>
/// <param name="oConnectionInfo">The connection info to set</param>
private void SetDBLogonForReport( ConnectionInfo oConnectionInfo )
{
try
{
TableLogOnInfos oTableLogOnInfos = ReportViewer.LogOnInfo;
foreach ( CrystalDecisions.CrystalReports.Engine.Table oTable in ReportSource.ReportDocument.Database.Tables )
{
if ( oTable.LogOnInfo.ConnectionInfo.ServerName == oConnectionInfo.ServerName )
{
TableLogOnInfo oTableLogOnInfo = oTable.LogOnInfo;
oTableLogOnInfo.ConnectionInfo = oConnectionInfo;
oTable.ApplyLogOnInfo( oTableLogOnInfo );
// oTable.Location = String.Format( "{0}.dbo.{1}", oConnectionInfo.DatabaseName, oTable.Name );
bool b = oTable.TestConnectivity();
if ( !b )
{
}
}
}
}
catch
{
throw;
}
}
/// <summary>
/// Configures the reports
/// </summary>
private void ConfigureReports()
{
try
{
//Get connection infos
ConnectionInfo sageConnectionInfo = this.GetConnectionInfo( new SqlConnectionStringBuilder( this.SageDatabase.ConnectString ), this.SageDataSourceName );
ConnectionInfo wapConnectionInfo = this.GetConnectionInfo( new SqlConnectionStringBuilder( ConfigurationManager.ConnectionStrings[ "DatabaseConnectString" ].ConnectionString ), this.WAPDataSourceName );
//Set db logon for the connection infos
this.SetDBLogonForReport( sageConnectionInfo );
this.SetDBLogonForReport( wapConnectionInfo );
}
catch
{
throw;
}
}
/// <summary>
/// Adds a discrete parameteer value
/// </summary>
/// <param name="ParameterName">The namee of the parameter to set</param>
/// <param name="value">The value of the parameter</param>
public void AddDiscreteValue( string ParameterName, object value )
{
try
{
//Create a new Parameter Field
CrystalDecisions.Shared.ParameterField oParameterField = new CrystalDecisions.Shared.ParameterField();
oParameterField.Name = ParameterName;
//Create a new Discrete Value
CrystalDecisions.Shared.ParameterDiscreteValue oParameterDiscreteValue = new CrystalDecisions.Shared.ParameterDiscreteValue();
oParameterDiscreteValue.Value = value;
//Add the value
oParameterField.CurrentValues.Add( oParameterDiscreteValue );
//Add the parameter field
this.ParameterFields.Add( oParameterField );
}
catch ( Exception )
{
throw;
}
}
/// <summary>
/// Sets up the Report Source
/// </summary>
private void SetReportSource()
{
try
{
//Load the report based on Filename
this.ReportSource.ReportDocument.Load( Server.MapPath( this.ReportFileName ) );
}
catch ( Exception )
{
throw;
}
}
/// <summary>
/// Exports the report to disk
/// </summary>
/// <param name="FileName">The name of the file</param>
public void ExportToDisk( string FileName )
{
try
{
this.ReportSource.ReportDocument.ExportToDisk( CrystalDecisions.Shared.ExportFormatType.PortableDocFormat, FileName );
}
catch ( Exception )
{
throw;
}
}
/// <summary>
/// Exports the Report to Email
/// </summary>
[Obsolete( "Bug in Crystal Reports objects that causes the attachment to be called untitled.txt", true )]
public void ExportToMAPI()
{
try
{
CrystalDecisions.Shared.ExportOptions oExportOptions = new CrystalDecisions.Shared.ExportOptions();
{
CrystalDecisions.Shared.MicrosoftMailDestinationOptions oMicrosoftMailDestinationOptions = new CrystalDecisions.Shared.MicrosoftMailDestinationOptions();
oMicrosoftMailDestinationOptions.MailToList = "nathanf@nfs.co.uk";
oMicrosoftMailDestinationOptions.MailSubject = "test";
oMicrosoftMailDestinationOptions.MailMessage = "Body text";
CrystalDecisions.Shared.PdfRtfWordFormatOptions oPdfRtfWordFormatOptions = new CrystalDecisions.Shared.PdfRtfWordFormatOptions();
oExportOptions.ExportDestinationOptions = oMicrosoftMailDestinationOptions;
oExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.MicrosoftMail;
oExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
oExportOptions.ExportFormatOptions = oPdfRtfWordFormatOptions;
this.ReportSource.ReportDocument.Export( oExportOptions );
}
}
catch ( Exception )
{
throw;
}
}
/// <summary>
/// Exports the Current Report to a Stream
/// </summary>
/// <returns></returns>
public Stream ExportToStream()
{
try
{
return this.ReportSource.ReportDocument.ExportToStream( CrystalDecisions.Shared.ExportFormatType.PortableDocFormat );
}
catch ( Exception )
{
throw;
}
}
/// <summary>
/// Sets the Parameters
/// </summary>
private void SetParameters()
{
try
{
if ( this.ParameterFields.Count > 0 )
{
//Set the Parameters
this.ReportViewer.ParameterFieldInfo = this.ParameterFields;
}
//Set the report source
this.ReportViewer.ReportSource = this.ReportSource;
}
catch ( Exception )
{
throw;
}
}
#endregion Methods
//---------------------------------------------------------------------------------------------------------------------------
}
}
Чтобы использовать это в коде:
/// <summary>
/// Page Load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load( object sender, EventArgs e )
{
try
{
if ( !Page.IsPostBack )
{
//Set the report filename
this.CrystalReportViewer.ReportFileName = @"~/Reports/WAP Std Sage PurchaseOrder.rpt";
if ( base.CurrentOrder != null )
{
//Set the Sage Database
this.CrystalReportViewer.SageDatabase = base.CurrentOrder.SageDatabase;
//Set Order ID parameter
this.CrystalReportViewer.AddDiscreteValue( "OrderID", base.CurrentOrder.OrderID );
}
}
}
catch ( Exception ex )
{
ErrorLogging.LogError( ex );
}
}
Основные методы дляпосмотрите на AddDiscreteValue и SetParameters в пользовательском элементе управления
РЕДАКТИРОВАТЬ:
Абстрактные подходящие методы:
/// <summary>
/// Sets the Parameters
/// </summary>
private void SetParameters()
{
try
{
if ( this.ParameterFields.Count > 0 )
{
//Set the Parameters
this.ReportViewer.ParameterFieldInfo = this.ParameterFields;
}
//Set the report source
this.ReportViewer.ReportSource = this.ReportSource;
}
catch ( Exception )
{
throw;
}
}
/// Adds a discrete parameteer value
/// </summary>
/// <param name="ParameterName">The namee of the parameter to set</param>
/// <param name="value">The value of the parameter</param>
public void AddDiscreteValue( string ParameterName, object value )
{
try
{
//Create a new Parameter Field
CrystalDecisions.Shared.ParameterField oParameterField = new CrystalDecisions.Shared.ParameterField();
oParameterField.Name = ParameterName;
//Create a new Discrete Value
CrystalDecisions.Shared.ParameterDiscreteValue oParameterDiscreteValue = new CrystalDecisions.Shared.ParameterDiscreteValue();
oParameterDiscreteValue.Value = value;
//Add the value
oParameterField.CurrentValues.Add( oParameterDiscreteValue );
//Add the parameter field
this.ParameterFields.Add( oParameterField );
}
catch ( Exception )
{
throw;
}
}