.net中的CrystalReport(水晶报表)控件使用笔记

来源:互联网 发布:天猫淘宝区别 编辑:程序博客网 时间:2024/06/05 18:17

        今天试了试vs2008中带有的CrystalReport控件,感觉非常强大,专业的果然不一样.以前项目中拼死拼活写的报表界面,现在可以轻松的一路next搞定.当然今天试的时候也出了些问题.下面就简要记录下.

        页面载入时报"您请求的报表需要更多信息".

        出现这个问题是我在用oracle的表时发生的,会出现一个界面,让你输入服务器,用户,密码.然后登陆后报表才会展现.我做了点试验.当使用没有密码的access数据库文件后.报表就可以直接展现.所以我猜大概是由于ole中无法记录密码.所以连接的时候要确认次密码才能连接数据库,导致出现登陆界面.上网查了查代码.有人提到用这段代码来解决:

        

  1. TableLogOnInfo logOnInfo = new TableLogOnInfo(); 
  2. ReportDocument rpdMy0= new ReportDocument(); 
  3. logOnInfo.ConnectionInfo.ServerName = "NNN";  
  4. logOnInfo.ConnectionInfo.Password="dong"
  5. logOnInfo.ConnectionInfo.DatabaseName="Lab";
  6. logOnInfo.ConnectionInfo.UserID = "sa";
  7. String path = Server.MapPath("CrystalReport2.rpt"); 
  8. rpdMy0.Load(path); 
  9. rpdMy0.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
  10. CrystalReportViewer1.ReportSource = rpdMy0;

 结果这段代码导致我浪费了大量的时间.根本达不到效果.但是也不报错.暂时怀疑是CrystalReportViewer1.ReportSource = rpdMy0除了问题,值没有赋过去.等会在跟踪下看是不是这么回事.

 

        真正解决问题的是这段代码:

  1.         string sql = "select * from archcat";
  2.         string ConnectionString = "Data Source=orcl;user=da;password=da;";
  3.         OracleConnection conn = new OracleConnection(ConnectionString);//创建一个新连接
  4.         DataSet ds = new DataSet();
  5.         conn.Open();
  6.         OracleDataAdapter ad = new OracleDataAdapter(sql, conn);
  7.         OracleCommand cmd = new OracleCommand(sql, conn);        
  8.         
  9.         ad.Fill(ds,"testTable");
  10.         conn.Close();
  11.         String path = Server.MapPath("CrystalReport1.rpt");
  12.         CrystalReportSource1.ReportDocument.Load(path);
  13.         //注意此处必需指明Dataset中的表的名称,否则会提示“您请求的报表需要更多信息.”
  14.         CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables["testTable"]);
  15.         CrystalReportSource1.DataBind();
  16.         CrystalReportViewer1.ReportSource = CrystalReportSource1;
  17.         CrystalReportViewer1.DataBind();

其实整个流程就是

1 从数据库读出表中数据放到一个dataset中

2 将dataset的数据传给CrystalReportSource.

3 重绑定,使控件使用新数据

 

(过程其实也类似于重绑定Gridview)

 

最后感谢http://wangshiying1971.blog.163.com/blog/static/2309538420089188377654/ 此文作者.正确代码其实就是从这篇文中来的

原创粉丝点击