65.9K
CodeProject 正在变化。 阅读更多。
Home

在 .NET 2005 中使用 Crystal Report 开发 Web 报告。

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.90/5 (14投票s)

2006年3月29日

2分钟阅读

viewsIcon

185711

downloadIcon

2330

该报告构建于 XML Schema 之上,数据在运行时绑定到报告。

Sample Image - ReportView.jpg

引言

在本文中,我尝试介绍了在 .NET 2005 中使用 Crystal Report 开发 Web 报告的整个周期。我没有花费任何精力来美化报告。该报告仅显示来自 SQL Server Northwind 数据库的 Employee 主数据。

该报告构建于 XML Schema 之上,数据在运行时绑定到报告。


要求

  1. Microsoft C#.NET 2005
  2. Windows 2000/2003/XP
  3. SQL Server 2000/2005(可选)。我使用 SQL Server 获取数据。如果 SQL Server 不可用,请更改 web.config 中的连接字符串以及 Default.aspx.cs 中与连接相关的语法。


创建您自己的报告

要开始使用报告,您需要启动 .NET 2005 Development Studio。

  1. 启动一个新的 ASP .NET 网站,选择位置为“文件系统”并提供一个路径。
  2. 执行“添加新项”,选择“XML Schema”。
       如下图所示添加元素。

Sample screenshot

         保存后,元素在 XML 编辑器中将如下所示         


<xs:element name="EmployeeID" type="xs:int" />
<xs:element name="LastName" type="xs:string" />
<xs:element name="FirstName" type="xs:string" />
<xs:element name="Title" type="xs:string" />
<xs:element name="BirthDate" type="xs:dateTime" />
<xs:element name="Address" type="xs:string" />
<xs:element name="City" type="xs:string" />
<xs:element name="Region" type="xs:string" />
<xs:element name="PostalCode" type="xs:string" />
<xs:element name="Country" type="xs:string" />
<xs:element name="HomePhone" type="xs:string" />

   3.  执行“添加新项”,选择“Crystal Report”。

    1. 为报告指定一个名称。
    2. 选择 -- 使用报表向导。 -- 确定。
    3. 将出现一个数据窗口,单击“创建新连接”,然后单击“ADO .NET”。
    4. 将出现一个连接窗口。 提供您刚刚创建的 XML Schema 文件。 完成。
    5. Schema 将显示在“ADO .NET”标签下。 选择它。 然后使用 > 按钮进行选择。
    6. 单击下一步。 将出现字段窗口。 使用 >> 按钮选择全部。
    7. 单击完成,以进行快捷操作。

    4.  在设计模式下打开 Default.aspx。 将 CrystalReportViewer 控件添加到其中。 源文件将如下所示。

 <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" /> 

    5.  打开 Default.aspx.cs 文件并粘贴以下代码。
 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//--SqlClient for SqlConnection and etc.
using System.Data.SqlClient;
//--for CrystalReports's ReportDocument.
using CrystalDecisions.CrystalReports.Engine;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//--Sql string
String strCmd = "";
strCmd += "Select EmployeeID, LastName, FirstName, Title, BirthDate, ";
strCmd += "Address, City, Region, PostalCode, Country, HomePhone ";
strCmd += "From Employees ";
//--Opening Sql Connection
string strConn = ConfigurationManager.AppSettings["connectionstring"];
SqlConnection sqlConn = new SqlConnection(strConn);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(strCmd, sqlConn);
//--this statement is very important, here the table name should
//--match with the XML Schema table name
da.Fill(ds, "Employees");
//--Closing Sql Connection
sqlConn.Close();
//--(Optional) I have used it to disable the properties
CrystalReportViewer1.DisplayGroupTree = false;
CrystalReportViewer1.HasCrystalLogo = false;
//--Initializing CrystalReport
ReportDocument myReportDocument;
myReportDocument = new ReportDocument();
myReportDocument.Load(Server.MapPath("Employees.rpt"));
myReportDocument.SetDataSource(ds);
//--Binding report with CrystalReportViewer
CrystalReportViewer1.ReportSource = myReportDocument;
CrystalReportViewer1.DataBind();
}
}

    6.  打开 Web.config 文件并根据您的机器(ip 地址、用户名、密码)更改 <appSettings> 下的连接字符串。


<appSettings>
<add key="connectionString" value="Data Source=9.182.223.80;Initial Catalog=Northwind;Persist Security Info=True;User ID=testUser;Password=password" />

</appSettings>
If in case the file is not available then perform "Add New Item", choose "Web Configuration File", and follow the same thing.

    7.  使用脚本

只是为了检查 Employees master 表是否可用以及是否包含数据
   . 打开 SQL Server 查询分析器
   . 复制脚本
   . 粘贴到查询分析器
   . 按 F5 执行

---------------------------------
-----Using Database
---------------------------------
Use Northwind;
---------------------------------
-----Selecting data from Table
---------------------------------
Select EmployeeID, LastName, FirstName, Title, BirthDate,
Address, City, Region, PostalCode, Country, HomePhone
From Employees;
    7.  现在构建 Web 项目。 按 F5/Ctrl F5 查看报告。 希望一切顺利。


 

© . All rights reserved.