zoukankan      html  css  js  c++  java
  • Microsoft Visual Studio 2005中使用水晶报表

    Microsoft Visual Studio 2005中使用水晶报表
    沈阳 王智
    OICQ:16994162
    2006-4-11
    如有转贴请注明出处
    水晶报表是一个功能强大的报表工具,现在已经被Microsoft Visual Studio 2005(下文以VS2005简称)集成在一起。喜欢水晶报表的朋友可以方便使用了。我把水晶报表在vs2005的使用方法总结一下,供大家参考。
    首先介绍一下我用的软件环境:Microsoft Visual Studio 2005;Microsoft SQL Server 2005
    【数据用例】
    服务器:SYWZSWL\SQLEXPRESS
    数据库名:Test
    数据库表:T
    数据:
     
    1
    【说明】
    水晶报表在应用时分两种方法,分别是拉模式(PULL)、推模式(PUSH)。拉模式:在水晶报表生成时的数据源是从水晶报表文件中的SQL语句从数据库中提取的,在编程时不用重写SQL语句,但要加上登录信息(具体方法,后面介绍)。推模式:在水晶报表生成时的数据源,是用编程时重写水晶报表中SQL语句而生成的dataset对像。也就是说,推模式是用dataset组装水晶报表。
    水晶报表组件介绍。水晶报表在VS2005中有两种组件,在WEB项目是分别是CrystalReportSource,CrystalReportViewer。在FORM项目里是分别是crystalReport,CrystalReportViewer。
    CrystalReportSource,crystalReport是水晶报表的数据提供者;CrystalReportViewer是水晶报表的浏览器。另外还要介绍一下水的报表的文件是以rpt为扩展名的文件,该文件可以用VS2005生成。
    下面分别介绍具体操作方法:
    拉模式(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;
        ///<summary>
        ///功能:拉模式提取水晶报表
        ///编写人:王智
        /// OICQ:16994162
        ///城市:沈阳
        ///日期:2006-4-13
        ///个人主页:http://www.sywangzhi.com/
        ///</summary>
        ///<param name="sender"></param>
        ///<param name="e"></param>
        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();
     
        }
    2
     
    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;
     
            }
    3
     
    推模式(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;User ID=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();
        }
    4
     
    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;User ID=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;
    }
     
    5
     
     


    Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=661778

  • 相关阅读:
    Realtime crowdsourcing
    maven 常用插件汇总
    fctix
    sencha extjs4 command tools sdk
    首次吃了一颗带奶糖味的消炎药,不知道管用不
    spring mvc3 example
    ubuntu ati driver DO NOT INSTALL recommand driver
    yet another js editor on windows support extjs
    how to use springsource tools suite maven3 on command
    ocr service
  • 原文地址:https://www.cnblogs.com/star250/p/626052.html
Copyright © 2011-2022 走看看