CrystalReport水晶报表

摘要如下:

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.csForm_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.csForm_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.csForm_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;

   }

posted on 2007-07-29 14:19  石川  阅读(351)  评论(0编辑  收藏  举报