水晶报表的PUSH模式与 PULL模式

来源:互联网 发布:罗马全面战争mac版 编辑:程序博客网 时间:2024/04/28 02:43

本文转自:http://www.cnblogs.com/greencolor/archive/2010/05/02/1726082.html

 

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

 

使用PUSH模式    
    我们采用下面的几步使用Push模式执行水晶报表:  
    1.   设计一个DataSet  
    2.   创建一个.rpt文件同时将其指定给上一步建立的DataSet。  
    3.   在aspx页面中拖放一个CrystalReportViewer控件同时将其与前面的rpt文件建立联系。  
    4.   在代码中访问数据库并把数据存入DataSet  
    5.   调用DataBind方法。  

 

 设计一个DataSet  
    1)   右击“解决方案浏览器”,选择“添加”--“添加新项”-->“数据集”  
    2)   从“服务器资源管理器”中的“SQL   Server”中拖放“ProjectStage”表(位于dbname数据库中)。           
    3)   此时在数据集中就会有一个ProjectStage表的结构图。       
    -   .xsd文件中仅仅包含一个结构图,但是不会有任何数据在里面。  
    创建   .rpt   文件   :  
    4)   使用上面的介绍过的方法创建此文件,唯一的不同就是使用数据集来代替前面的直接连接数据。  
    5)建立.rpt文件之后,右击“详细资料”-->"添加/删除数据库“  
    6)   在”数据库专家“窗口中,展开”项目数据“(代替以前的OleDb),展开“ADO.Net数据集”--"DataSet1“,选择”ProjectStage“表。  
    7)   将”ProjectStage"表添加到“选定的表”中,点击“OK”  
   
       Code   Behind   代码:  

PUSH model FORM
            string strProvider = "Server=.;DataBase=dbname;UID=sa;PWD=sa";
            CrystalReport1 oCR = new CrystalReport1();
            DataSet1 ds = new DataSet1();
            SqlConnection MyConn = new SqlConnection(strProvider);
            MyConn.Open();
            string strSel = "Select   *   from   ProjectStage";
            SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, MyConn);
            MyAdapter.Fill(ds, "ProjectStage");
            oCR.SetDataSource(ds);
            this.crystalReportViewer1.ReportSource = oCR;  

 

PULL model  FORM

            CrystalReport1.Load(Application.StartupPath + "CrystalReport1.rpt");
            CrystalReport1.SetDatabaseLogon("sa", "sa", "servername", "dbname");         
            crystalReportViewer1.ReportSource = CrystalReport1;

 

PUSH model WEB

         string sql = "Select * FROM ProjectStage";
        string DBConfig_sql ="Data Source=.;Initial Catalog=dbname;User ID=sa;Password=sa";
        DataSet1 ds = new DataSet1();
            SqlConnection sqlCon = new SqlConnection(DBConfig_sql);
            SqlCommand sqlCmd = new SqlCommand(sql, sqlCon);
            SqlDataAdapter sqlAd = new SqlDataAdapter();
            sqlAd.SelectCommand = sqlCmd;
            sqlAd.Fill(ds, "ProjectStage");
        CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport.rpt"));  
        CrystalReportSource1.ReportDocument.SetDataSource(ds.Tables["ProjectStage"]);
        CrystalReportSource1.DataBind();
        CrystalReportViewer1.ReportSource = CrystalReportSource1;
        CrystalReportViewer1.DataBind();

 

PULL model WEB

        CrystalReportSource1.ReportDocument.Load(Server.MapPath("CrystalReport.rpt"));
        CrystalReportSource1.ReportDocument.SetDatabaseLogon("sa", "sa", "servername", "aa");

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

 

 

原创粉丝点击