Dim crSections As Sections Dim crSection As Section Dim crReportObjects As ReportObjects Dim crReportObject As ReportObject Dim crSubreportObject As SubreportObject Dim crReportDocument As CrystalReport1 Dim crSubreportDocument As ReportDocument Dim crDatabase As Database Dim crTables As Tables Dim crTable As Table Dim crTableLogOnInfo As TableLogOnInfo Dim crConnectioninfo As ConnectionInfo#Region " Windows Form Designer generated code " Public Sub New(ByVal ServerName As String, ByVal UserID As String, ByVal Password As String, ByVal DatabaseName As String) MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'declare an instance of the report and the connectionInfo object crReportDocument = New CrystalReport1() crConnectioninfo = New ConnectionInfo() 'pass the necessary parameters to the connectionInfo object With crConnectioninfo .ServerName = ServerName .UserID = UserID .Password = Password .DatabaseName = DatabaseName End With 'set up the database and tables objects to refer to the current report crDatabase = crReportDocument.Database crTables = crDatabase.Tables 'loop through all the tables and pass in the connection info For Each crTable In crTables crTableLogOnInfo = crTable.LogOnInfo crTableLogOnInfo.ConnectionInfo = crConnectioninfo crTable.ApplyLogOnInfo(crTableLogOnInfo) Next 'set the crSections object to the current report's sections crSections = crReportDocument.ReportDefinition.Sections 'loop through all the sections to find all the report objects For Each crSection In crSections crReportObjects = crSection.ReportObjects 'loop through all the report objects to find all the subreports For Each crReportObject In crReportObjects If crReportObject.Kind = ReportObjectKind.SubreportObject Then 'you will need to typecast the reportobject to a subreport 'object once you find it crSubreportObject = CType(crReportObject, SubreportObject) 'open the subreport object crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName) 'set the database and tables objects to work with the subreport crDatabase = crSubreportDocument.Database crTables = crDatabase.Tables 'loop through all the tables in the subreport and 'set up the connection info and apply it to the tables For Each crTable In crTables With crConnectioninfo .ServerName = ServerName .DatabaseName = DatabaseName .UserID = UserID .Password = Password End With crTableLogOnInfo = crTable.LogOnInfo crTableLogOnInfo.ConnectionInfo = crConnectioninfo crTable.ApplyLogOnInfo(crTableLogOnInfo) Next End If Next Next 'view the report CrystalReportViewer1.ReportSource = crReportDocument Me.WindowState = FormWindowState.Maximized 'Add any initialization after the InitializeComponent() call End Sub
举个简单的例子:
员工表(员工编号 员工姓名 部门编号)
部门表(部门编号 部门名称)要求是: select 员工表.员工姓名,部门表.部门姓名 from 员工表,部门表 where 员工表.部门编号=部门表.部门编号操作步骤(列举几个比较重要 也是自己当时操作比较容易困惑的地方)
1.建xsd文件 直接拖入员工表和部门表 不要做任何字段关联
2.建rpt文件 选择员工表和部门表后 建立链接 员工表的部门编号---〉部门表的部门编号
3.建.aspx文件 拖入报表控件
4.在.aspx.cs中建立一个DataSet,里面是两张表,名称分别是员工表、部门表(和拖入XSD的名称保持 一致)
sql语句分别为:
select * from 员工表
select * from 部门表基本上就是这样 大家可以试着做一下 有什么问题可以一起讨论一下posted @ 9:21 | 评论 (0) | 收藏 2004年02月05日 # 水晶报表使用经验谈2--使用sql语句直接生成dataset做为报表的数据源!
上一次自己做了直接在rpt文件里使用oledb连接使用数据库的方法 但是不是很灵活 这次做了使用sql语句直接生成dataset做为报表的数据源(即push模式),这样就可以接受参数了。当然报表有设置参数的功能 这方面我还没有详细研究一.sql语句中没有使用表的相互关联(简单的查询语句)
设计一个DataSet
1) 右击“解决方案浏览器”,选择“添加”--“添加新项”-->“数据集”
2) 从“服务器资源管理器”中的“SQL Server”中拖放“Stores”表(位于PUBS数据库中)
3) 此时在数据集中就会有一个Stores表的结构图。xsd文件中仅仅包含一个结构图,但是不会有任何数据在里面
创建一个.rpt文件crystalreport1.rpt同时将其指定给上一步建立的DataSet。
4) 使用上面的介绍过的方法创建此文件,唯一的不同就是使用数据集来代替前面的直接连接数据。
5)建立.rpt文件之后,右击“详细资料”-->"添加/删除数据库“
6) 在”数据库专家“窗口中,展开”项目数据“(代替以前的OleDb),展开“ADO.Net数据集”--"DataSet1“,选择”Stores“表。
7) 将”Stores"表添加到“选定的表”中,点击“OK”
8) 建立一个WebForm1.aspx 拖入一个Crystal Report Viewer 控件
9)WebForm1.aspx.cs
ReportDocument oRpt = new ReportDocument();
string RptDir="f:\\bbs\\test\\crystal\\crystalreport2.rpt";
oRpt.Load(RptDir);
...//根据sql语句得到DataSet 这个就不多说了 oRpt.SetDataSource(ds);
CrystalReportViewer1.ReportSource=oRpt; //注意push模式用不到设置logOnInfo参数10)运行试试看!应该没问题哦
另外送上水晶报表10的下载:http://sc.fixdown.com/fixdown/download.asp?id=14867&free=sx-down
posted @ 11:22 | 评论 (1) | 收藏 2004年02月04日 # 水晶报表使用经验谈1--建立水晶报表第一步及编译最易出现错误的解决方法及报表转换成pdf文档进行打印方法
初用水晶报表(vs .net2003集成,版本Version=9.1.5000.0)
装好后要注册??? 注册号:6707437608??? 密码:AAP5GKS0000GDE100DS?
想要在.aspx文件中使用水晶报表
1.先新建文件webform1.aspx? 在页面拖入组件CrystalReportViewer? 生成代码2.在项目的同个文件夹中新建文件crystalreport1.rpt? 在字段资源管理器的数据库字段“添加数据库” 请使用oledb连接 从而选择你所要在报表中显示的数据表(有向导) 3.在webform1.aspx.cs主要代码如下:
protected CrystalDecisions.Web.CrystalReportViewer CrystalReportViewer1;?
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
TableLogOnInfo logOnInfo = new TableLogOnInfo ();
ReportDocument oRpt = new ReportDocument();
string RptDir="f:\\bbs\\test\\crystal\\crystalreport1.rpt";??? //crystalreport1.rpt文件所在的绝对路径
oRpt.Load(RptDir);//设置logOnInfo参数,注意这里如果不设?编译时最容易出现“登陆失败”的错误!
logOnInfo.ConnectionInfo.ServerName = "服务器名";
logOnInfo.ConnectionInfo.DatabaseName = "数据库名";
logOnInfo.ConnectionInfo.UserID = "用户名";
logOnInfo.ConnectionInfo.Password = "密码";
oRpt.Database.Tables [0].ApplyLogOnInfo(logOnInfo);//建立.rpt文件与CryStalReportviewer文件之间的连接
CrystalReportViewer1.ReportSource=oRpt;
}4.//生成pdf文档 在以上代码中加入
ExportOptions crExportOptions=new ExportOptions();
DiskFileDestinationOptions crDiskFileDestinationOptions=new DiskFileDestinationOptions();
crDiskFileDestinationOptions.DiskFileName="f:\\bbs\\test\\crystal\\crystalreport1.pdf";
crExportOptions=oRpt.ExportOptions ;
crExportOptions.DestinationOptions=crDiskFileDestinationOptions;
crExportOptions.ExportDestinationType =ExportDestinationType.DiskFile;
crExportOptions.ExportFormatType =ExportFormatType.PortableDocFormat ;oRpt.Export();
oRpt.Close();5.//读取报表导出的内容并传到客户端?继续加入以下代码
Response.ClearContent();
Response.ClearHeaders ();
Response.ContentType ="application/pdf";
Response.WriteFile("f:\\bbs\\test\\crystal\\crystalreport1.pdf");Response.Flush();
Response.Close();?
Dim crSection As Section
Dim crReportObjects As ReportObjects
Dim crReportObject As ReportObject
Dim crSubreportObject As SubreportObject Dim crReportDocument As CrystalReport1
Dim crSubreportDocument As ReportDocument Dim crDatabase As Database
Dim crTables As Tables
Dim crTable As Table
Dim crTableLogOnInfo As TableLogOnInfo
Dim crConnectioninfo As ConnectionInfo#Region " Windows Form Designer generated code " Public Sub New(ByVal ServerName As String, ByVal UserID As String, ByVal Password As String, ByVal DatabaseName As String)
MyBase.New() 'This call is required by the Windows Form Designer.
InitializeComponent() 'declare an instance of the report and the connectionInfo object crReportDocument = New CrystalReport1()
crConnectioninfo = New ConnectionInfo() 'pass the necessary parameters to the connectionInfo object
With crConnectioninfo
.ServerName = ServerName
.UserID = UserID
.Password = Password
.DatabaseName = DatabaseName
End With 'set up the database and tables objects to refer to the current report
crDatabase = crReportDocument.Database
crTables = crDatabase.Tables 'loop through all the tables and pass in the connection info
For Each crTable In crTables
crTableLogOnInfo = crTable.LogOnInfo
crTableLogOnInfo.ConnectionInfo = crConnectioninfo
crTable.ApplyLogOnInfo(crTableLogOnInfo)
Next 'set the crSections object to the current report's sections
crSections = crReportDocument.ReportDefinition.Sections 'loop through all the sections to find all the report objects
For Each crSection In crSections
crReportObjects = crSection.ReportObjects
'loop through all the report objects to find all the subreports
For Each crReportObject In crReportObjects
If crReportObject.Kind = ReportObjectKind.SubreportObject Then
'you will need to typecast the reportobject to a subreport
'object once you find it
crSubreportObject = CType(crReportObject, SubreportObject) 'open the subreport object
crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName) 'set the database and tables objects to work with the subreport
crDatabase = crSubreportDocument.Database
crTables = crDatabase.Tables 'loop through all the tables in the subreport and
'set up the connection info and apply it to the tables
For Each crTable In crTables
With crConnectioninfo
.ServerName = ServerName
.DatabaseName = DatabaseName
.UserID = UserID
.Password = Password
End With
crTableLogOnInfo = crTable.LogOnInfo
crTableLogOnInfo.ConnectionInfo = crConnectioninfo
crTable.ApplyLogOnInfo(crTableLogOnInfo)
Next End If
Next
Next 'view the report
CrystalReportViewer1.ReportSource = crReportDocument
Me.WindowState = FormWindowState.Maximized
'Add any initialization after the InitializeComponent() call End Sub