xugang 记录一个.NET 程序员的成长 asp.net水晶报表的一些问题

简介:

1。第一步是添加新项CrystalReport1
2。在CrystalReport1.rpt面页右键选择:”数据库”à”添加/删除数据库”出现下面对话框:
然后点开OLE DB(ADO)的加号,将弹出选择数据库的对话框
其中”项目数据”里面的”当前连接”显示的就是OLE DB(ADO)的连接,这样就可以将表添加到右边的”选定的表”中了(切记:不要既从”OLE DB” 中选表或命令又从”当前连接”中选,如果你用了数据集,也是一样的道理,否则全提示出”登陆失败”的错误!)
3。字段资料管理器里面”数据库字段”里把字段直接拖到CrystalReport.rpt里面了.布置好布局.
4。回到WebForm1.aspx页面,选择控件CrystalReportViewer将其放到页面上。
5。代码
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
在WebForm1.aspx.cs的Form_Load里面添加代码:
TableLogOnInfo logOnInfo = new TableLogOnInfo();
ReportDocument rpdMy0= new ReportDocument();
logOnInfo.ConnectionInfo.ServerName = "NNN";
logOnInfo.ConnectionInfo.Password="dong";
logOnInfo.ConnectionInfo.DatabaseName="Lab";
logOnInfo.ConnectionInfo.UserID = "sa";

String path = Server.MapPath("CrystalReport2.rpt");
rpdMy0.Load(path);
rpdMy0.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
CrystalReportViewer1.ReportSource = rpdMy0;
其中:
TableLogOnInfo类是: 提供属性,以便检索和设置表连接选项.它有一个连接的属性ConnectionInfo,用来设置与数据库的连接.如上面的ServerName等.生成一个报表文档对象将前面我们做好的那个CrystalReport1.rpt赋给它.在这里要注意的是Database属性!然后把报表对象做为CrystalReportViewer1的数据源.
或者,获得数据库的方法改为直接从数据库获得DataSet
using System.Data.SqlClient;
using System.Collections.Specialized;
string strConn=System.Configuration.ConfigurationSettings.AppSettings["DB"].ToString();
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlDataAdapter da = new SqlDataAdapter(strSQL,conn);//strSQL是SQL语言"select * from dbo.table1"
DataSet ds=new DataSet();
da.Fill( ds);
CrystalReport1 cr1 = new CrystalReport1();
cr1.SetDataSource( ds);
CrystalReportViewer1.ReportSource = cr1;
}
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
在WebForm1.aspx.cs的Form_Load里面添加代码:
TableLogOnInfo logOnInfo = new TableLogOnInfo();
ReportDocument rpdMy0= new ReportDocument();
logOnInfo.ConnectionInfo.ServerName = "NNN";
logOnInfo.ConnectionInfo.Password="dong";
logOnInfo.ConnectionInfo.DatabaseName="Lab";
logOnInfo.ConnectionInfo.UserID = "sa";

String path = Server.MapPath("CrystalReport2.rpt");
rpdMy0.Load(path);
rpdMy0.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
CrystalReportViewer1.ReportSource = rpdMy0;
其中:
TableLogOnInfo类是: 提供属性,以便检索和设置表连接选项.它有一个连接的属性ConnectionInfo,用来设置与数据库的连接.如上面的ServerName等.生成一个报表文档对象将前面我们做好的那个CrystalReport1.rpt赋给它.在这里要注意的是Database属性!然后把报表对象做为CrystalReportViewer1的数据源.
或者,获得数据库的方法改为直接从数据库获得DataSet
using System.Data.SqlClient;
using System.Collections.Specialized;
string strConn=System.Configuration.ConfigurationSettings.AppSettings["DB"].ToString();
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlDataAdapter da = new SqlDataAdapter(strSQL,conn);//strSQL是SQL语言"select * from dbo.table1"
DataSet ds=new DataSet();
da.Fill( ds);
CrystalReport1 cr1 = new CrystalReport1();
cr1.SetDataSource( ds);
CrystalReportViewer1.ReportSource = cr1;
}
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
在WebForm1.aspx.cs的Form_Load里面添加代码:
TableLogOnInfo logOnInfo = new TableLogOnInfo();
ReportDocument rpdMy0= new ReportDocument();
logOnInfo.ConnectionInfo.ServerName = "NNN";
logOnInfo.ConnectionInfo.Password="dong";
logOnInfo.ConnectionInfo.DatabaseName="Lab";
logOnInfo.ConnectionInfo.UserID = "sa";

String path = Server.MapPath("CrystalReport2.rpt");
rpdMy0.Load(path);
rpdMy0.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
CrystalReportViewer1.ReportSource = rpdMy0;
其中:
TableLogOnInfo类是: 提供属性,以便检索和设置表连接选项.它有一个连接的属性ConnectionInfo,用来设置与数据库的连接.如上面的ServerName等.生成一个报表文档对象将前面我们做好的那个CrystalReport1.rpt赋给它.在这里要注意的是Database属性!然后把报表对象做为CrystalReportViewer1的数据源.
或者,获得数据库的方法改为直接从数据库获得DataSet
using System.Data.SqlClient;
using System.Collections.Specialized;
string strConn=System.Configuration.ConfigurationSettings.AppSettings["DB"].ToString();
using (SqlConnection conn = new SqlConnection(strConn))
{
SqlDataAdapter da = new SqlDataAdapter(strSQL,conn);//strSQL是SQL语言"select * from dbo.table1"
DataSet ds=new DataSet();
da.Fill( ds);
CrystalReport1 cr1 = new CrystalReport1();
cr1.SetDataSource( ds);
CrystalReportViewer1.ReportSource = cr1;
}
如果你用的是水晶报表的推模式,一般不用设置登陆信息,但是要这样写:obj.SetDataSource(this.ds.Tables["tablename"]);如果你写成了obj.SetDataSource(this.ds)就会有登陆框的。

如果你用的是水晶报表的拉模式,你就一定要写上登陆信息:
crReportDocument = new OracleReport();
//Set the crConnectionInfo with the current values stored in the report
crConnectionInfo = crReportDocument.Database.Tables[0].LogOnInfo.ConnectionInfo;
/* Populate the ConnectionInfo Objects Properties with the appropriate values for
the ServerName, User ID, Password and DatabaseName. However, since Oracle
works on Schemas, Crystal Reports does not recognize or store a DatabaseName.
Therefore, the DatabaseName property must be set to a BLANK string. */
crConnectionInfo.DatabaseName = "";
crConnectionInfo.ServerName = "Your Server Name";
crConnectionInfo.UserID = "Your User ID";
crConnectionInfo.Password = "Your Password";
//Set the CrDatabase Object to the Report's Database
crDatabase = crReportDocument.Database;
//Set the CrTables object to the Tables collection of the Report's dDtabase
crTables = crDatabase.Tables;
//Loop through each Table object in the Tables collection and apply the logon info
//specified ealier. Note this sample only has one table so the loop will only execute once
foreach (Table crTable in crTables)
{
crTableLogOnInfo = crTable.LogOnInfo;
crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
crTable.ApplyLogOnInfo (crTableLogOnInfo);
// if you wish to change the schema name as well, you will need to set Location property as follows:
// crTable.Location = "<new schema name>." + crTable.Name;
}
//Set the ReportSource of the CrystalReportViewer to the strongly typed Report included in the project
crystalReportViewer1.ReportSource = crReportDocument;

还有一点要注意:
如果你用到了子报表,一定要处理:
//Go through each sections in the main report and identify the subreport by name
crSections = crReportDocument.ReportDefinition.Sections;
foreach(Section crSection in crSections)
{
crReportObjects = crSection.ReportObjects;
//loop through all the report objects to find all the subreports
foreach(ReportObject crReportObject in crReportObjects)
{
if (crReportObject.Kind == ReportObjectKind.SubreportObject)
{
//you will need to typecast the reportobject to a subreport
//object once you find it
crSubreportObject = (SubreportObject)crReportObject;
//open the subreport object
crSubReportDoc = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
//Once the correct subreport has been located pass it the
//appropriate dataset
if(crSubReportDoc.Name == "FirstSub")
{
//crSubReportDoc.Database.Tables[0].SetDataSource(ds);
crSubReportDoc.SetDataSource(ds);
}
}
}
}
crystalReportViewer1.ReportSource = crReportDocument;
同样crSubReportDoc.SetDataSource(ds);改为:crSubReportDoc.SetDataSource(ds.Tables["tablename"]);



本文转自钢钢博客园博客,原文链接:http://www.cnblogs.com/xugang/archive/2007/09/13/892037.html,如需转载请自行联系原作者

相关文章
|
7月前
|
开发框架 前端开发 .NET
VB.NET中如何利用ASP.NET进行Web开发
在VB.NET中利用ASP.NET进行Web开发是一个常见的做法,特别是在需要构建动态、交互式Web应用程序时。ASP.NET是一个由微软开发的开源Web应用程序框架,它允许开发者使用多种编程语言(包括VB.NET)来创建Web应用程序。
169 6
|
8月前
|
开发框架 JSON .NET
ASP.NET Core 标识(Identity)框架系列(三):在 ASP.NET Core Web API 项目中使用标识(Identity)框架进行身份验证
ASP.NET Core 标识(Identity)框架系列(三):在 ASP.NET Core Web API 项目中使用标识(Identity)框架进行身份验证
112 1
|
9月前
|
开发框架 搜索推荐 前端开发
【.NET全栈】ASP.NET开发Web应用——Web部件技术
【.NET全栈】ASP.NET开发Web应用——Web部件技术
|
8月前
|
开发框架 .NET 开发工具
【Azure 应用服务】App Service 的.NET Version选择为.NET6,是否可以同时支持运行ASP.NET V4.8的应用呢?
【Azure 应用服务】App Service 的.NET Version选择为.NET6,是否可以同时支持运行ASP.NET V4.8的应用呢?
|
8月前
|
开发框架 .NET 数据库连接
ASP.NET Core 标识(Identity)框架系列(一):如何使用 ASP.NET Core 标识(Identity)框架创建用户和角色?
ASP.NET Core 标识(Identity)框架系列(一):如何使用 ASP.NET Core 标识(Identity)框架创建用户和角色?
118 0
|
10月前
|
安全 程序员 Shell
老程序员分享:NSIS自定义界面,下载并安装Net.Framework4.8
老程序员分享:NSIS自定义界面,下载并安装Net.Framework4.8
|
10月前
|
Linux C# C++
【.NET Developer】创建ASP.NET Core Blazor项目并打包为Linux镜像发布到Azure应用服务
本文介绍了如何使用VS2019和.NET框架创建一个Blazor应用,并将其部署到Azure应用服务。首先,Blazor是一个使用C#而非JavaScript构建交互式Web UI的框架,支持共享服务器和客户端应用逻辑,以及与Docker和Azure集成。任务包括创建Blazor项目,配置Dockerfile为Linux容器,本地测试,发布到Azure Container Registry (ACR),然后在Azure App Service for Container上部署。在部署过程中,需确保Docker设置正确,开启ACR的Admin访问权限,并监控镜像拉取和容器启动日志。
386 0
|
前端开发 JavaScript .NET
一起谈.NET技术,ASP.NET MVC2实现分页和右键菜单
  右键菜单非常方便,很多时候会用到。这篇文章将使用一个JQUERY的插件在asp.net mvc中实现右键菜单。本文还将介绍一下在asp.net mvc中如何实现简单的分页。效果如下图:   首先,下载此插件。
1024 1
|
前端开发 NoSQL .NET
一起谈.NET技术,重构TekPub——从ASP.NET MVC框架迁移到Ruby on Rails
  TekPub是一个面向开发人员的站点,致力于为开发人员提供一系列主题的在线培训,主题范围非常广泛,从微软的O/R Mapping框架Microsoft Entity Framework,到如何使用Ruby on Rails技术编写自己的日志引擎等内容都有涉及。
1662 0
|
存储 缓存 .NET
一起谈.NET技术,提高ASP.NET应用程序性能的十大方法
  一、返回多个数据集   检查你的访问数据库的代码,看是否存在着要返回多次的请求。每次往返降低了你的应用程序的每秒能够响应请求的次数。通过在单个数据库请求中返回多个结果集,可以减少与数据库通信的时间,使你的系统具有扩展性,也可以减少数据库服务器响应请求的工作量。
1249 0