在很多的应用程序中,报表是不可缺少的,一张好的报表能直观地让人把握数据的情况,方便决策。在这篇文章中,我们将以一个三层结构的asp.net程序为例,介绍如何使用crystal report ,来制作一份报表,其中介绍了不少asp.net和水晶报表的技巧。
在这个例子中,我们设想的应用要为一个销售部门制作一份报表,管理者可以查看某段时间之内的销售情况,以列表或者折线图的形式反映出销售的趋势。我们将使用SQL Server 2000做为数据库,使用VB.NET编写中间层逻辑层,而前端的表示层使用C#。我们先来看下数据库的结构。
CREATE TABLE [dbo].[tblItem] ( [ItemId] [int] NOT NULL , [Description] [varchar] (50) NOT NULL ) ON [PRIMARY] CREATE TABLE [dbo].[tblSalesPerson] ( [SalesPersonId] [int] NOT NULL , [UserName] [varchar] (50) NOT NULL , [Password] [varchar] (30) NOT NULL ) ON [PRIMARY] CREATE TABLE [dbo].[tblSales] ( [SaleId] [int] IDENTITY (1, 1) NOT NULL , [SalesPersonId] [int] NOT NULL , [ItemId] [int] NOT NULL , [SaleDate] [datetime] NOT NULL , [Amount] [int] NOT NULL ) ON [PRIMARY] |
ALTER TABLE tblItem ADD CONSTRAINT PK_ItemId PRIMARY KEY (ItemId) GO ALTER TABLE tblSalesPerson ADD CONSTRAINT PK_SalesPersonId PRIMARY KEY (SalesPersonId) GO ALTER TABLE tblSales ADD CONSTRAINT FK_ItemId FOREIGN KEY (ItemId) REFERENCES tblItem(ItemId) GO ALTER TABLE tblSales ADD CONSTRAINT FK_SalesPersonId FOREIGN KEY (SalesPersonId) REFERENCES tblSalesPerson(SalesPersonId) GO |
Public Function GetAllItems () As Collections.ArrayList |
Public Function ValidateUser (strUserName as String, strPassword as String) As Integer |
Public Function GetSales (Optional nSaleId As Integer = 0, Optional nSalesPersonId As Integer = 0,Optional nItemId As Integer = 0) As Collections.ArrayList |
Public Function AddSale (objSale As Sale) |
<input type="image" onclick="Page_ValidationActive=false;" src="datepicker.gif" alt="Show Calender" runat="server" onserverclick="ShowCal1" id="ImgCal1" name="ImgCal1"> |
public void ShowCal1(Object sender, System.Web.UI.ImageClickEventArgs e) { //显示日历控件 DtPicker1.Visible = true; } |
private void DtPicker1_SelectionChanged(object sender, System.EventArgs e) { txtStartDate.Text = DtPicker1.SelectedDate.ToShortDateString(); DtPicker1.Visible = false; } |
private void bSubmit_ServerClick(object sender, System.EventArgs e) { Response.Redirect("ViewReport.aspx?ItemId=" + cboItemType.SelectedItem.Value + "&StartDate=" + txtStartDate.Text + "&EndDate=" + txtEndDate.Text);} |
名称: | 类型: |
ItemId | Number |
StartDate | Date |
EndDate | Date |
CrystalDecisions.CrystalReports.Engine CrystalDecisions.Shared 在viewreport.aspx的Page_load事件中,加入以下代码 //接收传递的参数 nItemId = int.Parse(Request.QueryString.Get("ItemId")); strStartDate = Request.QueryString.Get("StartDate"); strEndDate = Request.QueryString.Get("EndDate"); //声明报表的数据对象 CrystalDecisions.CrystalReports.Engine.Database crDatabase; CrystalDecisions.CrystalReports.Engine.Table crTable; TableLogOnInfo dbConn = new TableLogOnInfo(); // 创建报表对象opt ReportDocument oRpt = new ReportDocument(); // 加载已经做好的报表 oRpt.Load("F:\\aspnet\\WroxWeb\\ItemReport.rpt"); //连接数据库,获得相关的登陆信息 crDatabase = oRpt.Database; //定义一个arrtables对象数组 object[] arrTables = new object[1]; crDatabase.Tables.CopyTo(arrTables, 0); crTable = (CrystalDecisions.CrystalReports.Engine.Table)arrTables[0]; dbConn = crTable.LogOnInfo; //设置相关的登陆数据库的信息 dbConn.ConnectionInfo.DatabaseName = "WroxSellers"; dbConn.ConnectionInfo.ServerName = "localhost"; dbConn.ConnectionInfo.UserID = "sa"; dbConn.ConnectionInfo.Password = "test"; //将登陆的信息应用于crtable表对象 crTable.ApplyLogOnInfo(dbConn); //将报表和报表浏览控件绑定 crViewer.ReportSource = oRpt; //传递参数 setReportParameters(); |
oRpt.Load("F:\\aspnet\\WroxWeb\\ItemReport.rpt"); |
private void setReportParameters() { // all the parameter fields will be added to this collection ParameterFields paramFields = new ParameterFields(); // the parameter fields to be sent to the report ParameterField pfItemId = new ParameterField(); ParameterField pfStartDate = new ParameterField(); ParameterField pfEndDate = new ParameterField(); // 设置在报表中,将要接受的参数字段的名称 pfItemId.ParameterFieldName = "ItemId"; pfStartDate.ParameterFieldName = "StartDate"; pfEndDate.ParameterFieldName = "EndDate"; ParameterDiscreteValue dcItemId = new ParameterDiscreteValue(); ParameterDiscreteValue dcStartDate = new ParameterDiscreteValue(); ParameterDiscreteValue dcEndDate = new ParameterDiscreteValue(); dcItemId.Value = nItemId; dcStartDate.Value = DateTime.Parse(strStartDate); dcEndDate.Value = DateTime.Parse(strEndDate); pfItemId.CurrentValues.Add(dcItemId); pfStartDate.CurrentValues.Add(dcStartDate); pfEndDate.CurrentValues.Add(dcEndDate); paramFields.Add(pfItemId); paramFields.Add(pfStartDate); paramFields.Add(pfEndDate); // 将参数集合绑定到报表浏览控件 crViewer.ParameterFieldInfo = paramFields; } |
ParameterFields paramFields = new ParameterFields(); ParameterField pfItemId = new ParameterField(); ParameterField pfStartDate = new ParameterField(); ParameterField pfEndDate = new ParameterField(); // 设置在报表中,将要接受的参数字段的名称 pfItemId.ParameterFieldName = "ItemId"; pfStartDate.ParameterFieldName = "StartDate"; pfEndDate.ParameterFieldName = "EndDate"; |
ParameterDiscreteValue dcItemId = new ParameterDiscreteValue(); ParameterDiscreteValue dcStartDate = new ParameterDiscreteValue(); ParameterDiscreteValue dcEndDate = new ParameterDiscreteValue(); dcItemId.Value = nItemId; dcStartDate.Value = DateTime.Parse(strStartDate); dcEndDate.Value = DateTime.Parse(strEndDate); |
相关视频
相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么
热门文章 360快剪辑怎么使用 36金山词霸如何屏幕取词百度收购PPS已敲定!3
最新文章
微信3.6.0测试版更新了微信支付漏洞会造成哪
360快剪辑怎么使用 360快剪辑软件使用方法介酷骑单车是什么 酷骑单车有什么用Apple pay与支付宝有什么区别 Apple pay与贝贝特卖是正品吗 贝贝特卖网可靠吗
人气排行 xp系统停止服务怎么办?xp系统升级win7系统方电脑闹钟怎么设置 win7电脑闹钟怎么设置office2013安装教程图解:手把手教你安装与qq影音闪退怎么办 QQ影音闪退解决方法VeryCD镜像网站逐个数,电驴资料库全集同步推是什么?同步推使用方法介绍QQ2012什么时候出 最新版下载EDiary——一款好用的电子日记本
查看所有0条评论>>