在 Visual Studio .NET中使用Crystal Report(下)

** 在 ** ** Visual Studio .NET ** ** 中使用 ** ** Crystal Report( ** ** 下 ** ** ) **

from www.aspfree.com
translated by cash( 天下第七 )
[email protected]

** Crystal Report ** ** 演示-使用 ** ** Push Model **

下面看看如何使用 Push Model 实现 Crystal Reports

1. 创建一个设计时的 dataset

2. 创建一个 .rpt 文件并指向我们前面创建的 dataset

3. 在 .aspx 页面上放置 Crystal Report Viewer 控件,设定它的属性指向上一步创建的 .rpt 文件。

4. 在 code behind page 中,书写连接数据库的函数

5. 加上 databind 方法。

** 创建一个设计时的 ** ** dataset ** ** 去定义 ** ** Reports ** ** 的 ** ** Fielsds. **


  1. 在 "Solution Explorer" 右击,选择 "Add" --> select "Add New Item--> Select "DataSet"

  1. 从 "Server Explorer" 面板中的 "SQL Server" 中 拖进 "Stores" 表

  1. 这将在 dataset 中创建一个 "Stores" table

用这种方法创建的 .xsd 文件仅仅包含了 field 的定义,里面没有任何数据。需要你创建一个与数据库的链接并且将数据填充进去。


** 创建 ** ** .rpt ** ** 文件 **


  1. 创建一个 .rpt 文件。与前面唯一不同的是不通过 Crystal Report 得到表,我们将用 dataset 来创建它。

  2. 建立 .rpt 文件后,右击 "Details" section ,选择 "Add/Remove Database"

  3. 在 "Database Expert" 窗口,展开 "Project Data" ,展开 "ADO.NET DataSet","DataSet1", 选择 "Stores" table.

  4. 点击 ">" 将 "Stores" table 包括进 "Selected Tables"

  1. 接下来设定 report 的布局。

** 创建一个 ** ** Crystal Report Viewer Control **


  1. 接下来的步骤是用 PULL Model 创建一个 Crystal Report viewer Control 并设定它的属性。

** 改 ** ** code behind page ** ** 代码: **


  1. 为你的 page load 里设计如下了程序:

Sub BindReport()

Dim myConnection As New SqlClient.SqlConnection()

myConnection.ConnectionString= "server= (local)\NetSDK;database=pubs;Trusted_Connection=yes"

Dim MyCommand As New SqlClient.SqlCommand()

MyCommand.Connection = myConnection

MyCommand.CommandText = "Select * from Stores"

MyCommand.CommandType = CommandType.Text

Dim MyDA As New SqlClient.SqlDataAdapter()

MyDA.SelectCommand = MyCommand

Dim myDS As New Dataset1()

'This is our DataSet created at Design Time

MyDA.Fill(myDS, "Stores")

'You have to use the same name as that of your Dataset that you created during design time

Dim oRpt As New CrystalReport1()

' This is the Crystal Report file created at Design Time

oRpt.SetDataSource ( myDS )

' Set the SetDataSource property of the Report to the Dataset

CrystalReportViewer1.ReportSource = oRpt

' Set the Crystal Report Viewer's property to the oRpt Report object that we created

End Sub

注意:在上面的代码中,你可能会注意到 oRpt 对象是 "Strongly Typed" Report file 的一个实例。 如果我们用 "UnTyped" Report ,我们将不得不使用 ReportDocument 对象并且手工 load 这个 report 文件进去。

** 运行你的程序 **


  1. F5 运行。

** 输出 ** ** report ** ** 文件到另一种格式 **


你可以选择将你的 report 文件输出成以下格式:

1. PDF (Portable Document Format)

2. DOC (MS Word Document)

3. XLS (MS Excel Spreadsheet)

4. HTML (Hyper Text Markup Language – 3.2 or 4.0 compliant)

5. RTF (Rich Text Format)

事实上,你可以放置一个 button 来引发一个输出函数。

** 输出一个以 ** ** Pull Model ** ** 创建的 ** ** report ** ** 文件 **


当输出一个以 Pull Model 创建的 report 文件的时候, Crystal Report 对与数据库的连接及所需的记录很敏感,所以你只可使用下面提供的代码:

Private Sub Button1_ Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim myReport <SPAN lang=EN-US style="FONT-SIZE: 10pt; BACKGROUND: silver; COLOR: blue; FONT

Published At
Categories with Web编程
Tagged with
comments powered by Disqus