水晶报表在VS中的使用

水晶报表在应用时分两种方法,分别是拉模式(PULL)、推模式(PUSH)。

1、拉模式:
    在水晶报表生成时的"数据源"是从水晶报表文件中的SQL语句从数据库中提取的,在编程时不用重写SQL语句,
但要加上登录信息(具体方法,后面介绍)。

2、推模式:
    在水晶报表生成时的数据源,是用编程时重写水晶报表中SQL语句而生成的dataset对像。
也就是说,推模式是用dataset组装水晶报表。

3、水晶报表组件介绍:
    水晶报表在VS2005中有两种组件,在WEB项目是分别是CrystalReportSource,CrystalReportViewer。
在FORM项目里是分别是crystalReport,CrystalReportViewer。CrystalReportSource,crystalReport是水晶报表的数据提供者;
CrystalReportViewer是水晶报表的浏览器。另外还要介绍一下水的报表的文件是以rpt为扩展名的文件,该文件可以用VS2005生成。

4、具体操作方法:
(1)、拉模式(PULL):
 在拉模式中如要在水晶报表中的SQL语句加上条件参数时要用{?参数名}方式给出。
例:“SELECT T1, T2, T3 FROM T Where T1='{?parm}'” parm就是参数名
以下例子中所用到的水晶报表文件中使用的SQL语句是“SELECT T1, T2, T3 FROM T Where T1='{?parm}'” parm就是参数名。
【WEB方式下】
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;

protected void Button_pull_Click(object sender, EventArgs e)
{
// CrystalReport.rpt是水晶报表文件的名称;CrystalReportSource1是从工具箱加到页面上的水晶报表数据源对像。

CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport.rpt"));
// SetDatabaseLogon 拉模式中必须用这个方法来设置登录信息,参数一:用户名;参数二:密码;参数三:服务器;参数四:数据库名
CrystalReportSource1.ReportDocument.SetDatabaseLogon("sa", "123456", @"SYWZSWL/SQLEXPRESS", "Test");
//给水晶报表传参数,参数一:是参数名,参数二:参数值;
CrystalReportSource1.ReportDocument.SetParameterValue("Title", "这是一个测试报表");
CrystalReportSource1.ReportDocument.SetParameterValue("Parm", "1");

//绑定水晶报表数据源。
CrystalReportSource1.DataBind();
// CrystalReportViewer1是水晶报表浏览器,下面是给该浏览器赋上对像
CrystalReportViewer1.ReportSource = CrystalReportSource1;
CrystalReportViewer1.DataBind();

}

【FORM方式下】
//在FORM方式下代码同WEB方式,用crystalReport控件换掉了CrystalReportSource;用crystalReportViewer换掉了CrystalReportViewer;这两个控件都可以在工具箱里找到。同时在编程时去掉DataBind()方法。
private void Form1_Load(object sender, EventArgs e)
{

crystalReport1.Load(Application.StartupPath + "CrystalReport.rpt");

crystalReport1.SetDatabaseLogon("sa", "123456", @"SYWZSWL/SQLEXPRESS", "Test");

crystalReport1.SetParameterValue("Title", "这是一个测试报表");
crystalReport1.SetParameterValue("Parm", "1");
crystalReportViewer1.ReportSource = crystalReport1;

}


推模式(PUSH):
在推模式中编程组装的Dataset里的SQL语句中的字段要与水晶报表里的SQL语句字段一致。
简单的说,推模式中的水晶报表是个模板,把在设计器里报表的格式设好后,再组装DataSet就可以生成报表了。


【WEB方式下】

using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using System.Data.SqlClient;
protected void Button_push_Click(object sender, EventArgs e)
{
string sql = "SELECT T1, T2, T3 FROM T where T1='a'";
string DBConfig_sql =@"Data Source=SYWZSWL/SQLEXPRESS;Initial Catalog= Test;UserID=sa;Password=123456";
DataSet ds = new DataSet();
SqlConnection sqlCon = new SqlConnection(DBConfig_sql);
SqlCommand sqlCmd = new SqlCommand(sql, sqlCon);
SqlDataAdapter sqlAd = new SqlDataAdapter();
sqlAd.SelectCommand = sqlCmd;
sqlAd.Fill(ds, "sql");

CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport.rpt"));
//注意此处必需指明Dataset中的表的名称,否则会提示“您请求的报表需要更多信息.”
CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables["sql"]);
//{?}中的参数可以不用赋值,即使赋了值也不起作用。
// CrystalReportSource1.ReportDocument.ParameterFields["Parm"].CurrentValues.AddValue("1234567");
CrystalReportSource1.ReportDocument.ParameterFields["Title"].CurrentValues.AddValue("这时推模式的报表样例!");
CrystalReportSource1.DataBind();

CrystalReportViewer1.ReportSource = CrystalReportSource1;
CrystalReportViewer1.DataBind();
}
 

【FORM方式下】
private void Form1_Load(object sender, EventArgs e)
{
//推模式
string sql = "SELECT T1, T2, T3 FROM T where T1='a'";
string DBConfig_sql = @"Data Source=SYWZSWL/SQLEXPRESS;Initial Catalog= Test;UserID=sa;Password=123456";
DataSet ds = new DataSet();
SqlConnection sqlCon = new SqlConnection(DBConfig_sql);
SqlCommand sqlCmd = new SqlCommand(sql, sqlCon);
SqlDataAdapter sqlAd = new SqlDataAdapter();
sqlAd.SelectCommand = sqlCmd;
sqlAd.Fill(ds, "sql");
crystalReport1.Load(Application.StartupPath + "CrystalReport.rpt");
crystalReport1.SetDataSource(ds.Tables["sql"]);
//{?}中的参数可以不用赋值,即使赋了值也不起作用。
// CrystalReportSource1.ReportDocument.ParameterFields["Parm"].CurrentValues.AddValue("1234567");
crystalReport1.ParameterFields["Title"].CurrentValues.AddValue("这时推模式的报表样例!");

crystalReportViewer1.ReportSource = crystalReport1;
}

--------------------------------------------------------

2、水晶报表最简便绑定法
水晶报表一般在建立数据集之后进行绑定,下面我介绍一种,直接用Sql语句绑定,不需要设置CrystalReportViewer1的报表路径,不需要单独建立数据集的简便方法。

代码如下:

  CrystalReport3 oCR = new CrystalReport3(); // 绑定的.rpt 对象

   // sql绑定数据
   SqlConnection cnn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"]);
   SqlDataAdapter cmd1 = new SqlDataAdapter("select top 1 * from meetingplan",cnn);
   DataSet ds = new DataSet();
   cmd1.Fill(ds,"meetingplan");   

   oCR.SetDataSource(ds);
   this.CrystalReportViewer1.ReportSource = oCR;  // 绑定rpt文件
   cnn.Close();

注:meetingplan要与报表专家CrystalReport3绑定的一致,否则会出现查询引擎错误。

 

7-5 共7个压缩包(只有50M上传权限) Create rich reports within the familiarity of your Visual Studio development environment – for free Hit your report development deadlines and come in under budget without leaving Microsoft Visual Studio. Our report design software installs directly into Visual Studio. With this fully functional – and free software, you'll spend less and save time developing rich, interactive reports. Save time using powerful report creation, integration, and delivery tools Deliver interactive, graphical reports on any device through an XML Web services model Distribute highly formatted reports in rich-client Windows environments Enjoy flexible data access with support for over 35 data sources, major browsers and operating systems Extend your application with seamless report integration into WPF applications Seamless upgrade to SAP Crystal Reports 2011 for added report functionality Download SAP Crystal Reports, developer version for Microsoft Visual Studio now › Share 2 Features and Functionality Collapse All Features and Functionality Learn more about the features and functions of SAP Crystal Reports, developer version for Microsoft Visual Studio. Powerful designer features for creating rich reports Flexible Windows and Web application features support development ASP.NET features support your ASP development efforts Broad data access enables multiple sources and dynamic data output Platform features provide support and management for SAP Crystal Reports projects Get all the details on SAP Crystal Reports, developer version for Microsoft Visual Studio features › Less Technical & License Resources Discover if SAP Crystal Reports, developer version for Microsoft Visual Studio is right for you. Get started by reviewing the technical, data access and license details. Access virtually any data sources via ODBC, OLE DB or native connections Licensed for report design, runtime distribution and report viewer distribution Add reporting to thick client and intern
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值