<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />6-1-2 <?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />案例学习:打印某高校学生成绩单(动态数据)案例

要想获取图6-14中某门课程某个班级的成绩表,除了必须获取课程号和班级号这两个主键以外,还必须获取的信息包括:某门课程某个班级教师阅卷时候设定的考试成绩百分比值,以及参加该门课程考试的学生各个考试类型具体的成绩,同时也必须获取相应的学期信息、课程名、专业名、班级名,而且在系统运行最后应当将考试成绩的统计资料信息一并显示出来。因此,完成此案例涉及知识面广,设计程序代码比较复杂。

 

u 实验步骤1

由于该项目设计数据库复杂操作,故而提前建立两个类文件:BuessinessLayer.csdatabaselayer.cs,分别代表业务逻辑部分代码和数据库部分代码。有关databaselayer.cs的封装方法,在ADO.NET章节已经有所论述,此处不再展开;对于BuessinessLayer.cs中用到的方法,随着案例的推进将注意讲解分析。
6-15   建立 业务逻辑类和数据库类文件
u 实验步骤2

将上一节建立的rpt文件拷贝到当前WinForm项目的执行目录内(即当前项目的\bin\Debug\目录下面)。同时再建立一个新的Form窗体,该窗体主要实现通过选择班级和课程显示课程水晶报表的功能,其界面如图6-16所示。

6-16   通过选择班级和课程显示课程水晶报表
在当前窗体的Load事件中键入如下代码:
        /// <summary>

        /// 窗体加载事件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void Form4_Load(object sender, EventArgs e)

        {

            //班级信息加载

            string sql1 = "select bno,bname from class";

            DataSet ds1 = DB.GetDataSet(sql1);

            comboBox1.DataSource = ds1.Tables[0].DefaultView;

            comboBox1.DisplayMember = "bname";

            comboBox1.ValueMember = "bno";

            //课程信息加载

            string sql2 = "select cno,cname from course";

             DataSet ds2 = DB.GetDataSet(sql2);

            comboBox2.DataSource = ds2.Tables[0].DefaultView;

            comboBox2.DisplayMember = "cname";

            comboBox2.ValueMember = "cno";

}

双击窗体的显示水晶报表按钮,进入按钮单击事件:
        /// <summary>

        /// 准备打开水晶报表文件

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button1_Click(object sender, EventArgs e)

        {

            object bno = this.comboBox1.SelectedValue;

            object cno = this.comboBox2.SelectedValue;

            string s = "select scno from sc where cno=" + cno.ToString() + " and bno=" + bno.ToString();

            //查询是否选择的课程编号和班级编号有成绩信息

            if (DB.GetdataRow("select scno from sc where cno=" + cno.ToString() + " and bno=" + bno.ToString()) > 0)

            {

                Form1 f = new Form1(cno, bno);

                //重载Show方法,打开form1时候,传递cno 课程号和 bno 班级号参数过去

                f.Show();

            }

            else

            {

                MessageBox.Show("该班级该课程无成绩信息!");

            }

     }

u 实验步骤3

打开Form1窗体,该窗体仅仅有一个crystalReportViewer控件,用来显示来自水晶报表的数据信息。

6-17  Form1 窗体
进入Form1窗体的代码编辑文件,键入如下代码:
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using CrystalDecisions.Shared;

using CrystalDecisions.CrystalReports.Engine;

//注意:必须增加水晶报表的这两个命名空间才可以在编程状态下操作水晶报表对象。

namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        Buessiness business = new Buessiness();//实例化业务逻辑层,具体方法在下面分述介绍

        Mydatabase db = new Mydatabase();//实例数据库层,具体方法在ADO.NET已有介绍

        //此处的重载,接收来自外部的系统参数,课程号cno和班级号bno

        public Form1(object cno,object bno)

        {

            InitializeComponent();

            //初始化界面各个参数

            int percent_qm, percent_sj, percent_ps, ctime;

            //获取表头三项成绩百分比。

            business.GetCoursecheck(Convert.ToInt32(cno), Convert.ToInt32(bno), out percent_qm, out percent_sj, out  percent_ps);

            //第一个业务逻辑层的方法,请见下面的介绍。

            string xqmess, cname, zy, nj, bname, tname, year1, month1, day1, clx_string;

            Int16 clx_num;

            //获取表头标题各项信息

            business.GetPaperMessage(Convert.ToInt32(cno), Convert.ToInt32(bno), 2, out xqmess, out cname, out zy, out nj, out bname, out tname, out year1, out month1, out day1, out clx_num, out clx_string, out ctime);

            //第二个业务逻辑层的方法,请见下面的介绍。

            //获取表尾统计信息

            int first, second, third, forth, fifth, total1;

            business.GetPjStatistics(Convert.ToInt32(cno), Convert.ToInt32(bno), out first, out second, out third, out forth, out fifth, out total1);

             //第三个业务逻辑层的方法,请见下面的介绍。

            //注意学习如何获取磁盘内的水晶报表的rpt文件

            string path = Environment.CurrentDirectory + "\\CrystalReport1.rpt";

            ReportDocument report = new ReportDocument();

            report.Load(path);//加载报表

 

            ((TextObject)report.ReportDefinition.ReportObjects["Text_title1"]).Text = xqmess;

            ((TextObject)report.ReportDefinition.ReportObjects["Text_cname"]).Text = cname;

            ((TextObject)report.ReportDefinition.ReportObjects["Text3"]).Text = zy;

            ((TextObject)report.ReportDefinition.ReportObjects["Text5"]).Text = bname;

 

            //下面是表尾部分:

            ((TextObject)report.ReportDefinition.ReportObjects["Text_yx"]).Text = first.ToString();

            ((TextObject)report.ReportDefinition.ReportObjects["Text_lh"]).Text = second.ToString();

            ((TextObject)report.ReportDefinition.ReportObjects["Text_zd"]).Text = third.ToString();

            ((TextObject)report.ReportDefinition.ReportObjects["Text_jg"]).Text = forth.ToString();

            ((TextObject)report.ReportDefinition.ReportObjects["Text_nojg"]).Text = fifth.ToString();

            ((TextObject)report.ReportDefinition.ReportObjects["Text_totalnum"]).Text = total1.ToString();

 

            //下面是显示标头各个分值的百分比

            ((TextObject)report.ReportDefinition.ReportObjects["Text19"]).Text = percent_ps.ToString();

            ((TextObject)report.ReportDefinition.ReportObjects["Text21"]).Text = percent_sj.ToString();

            ((TextObject)report.ReportDefinition.ReportObjects["Text25"]).Text = percent_qm.ToString();

 

            //下面是写年月日信息

            ((TextObject)report.ReportDefinition.ReportObjects["Text38"]).Text = year1.ToString();

            ((TextObject)report.ReportDefinition.ReportObjects["Text39"]).Text = month1.ToString();

            ((TextObject)report.ReportDefinition.ReportObjects["Text40"]).Text = day1.ToString();

            ((TextObject)report.ReportDefinition.ReportObjects["Text43"]).Text = year1.ToString();

            ((TextObject)report.ReportDefinition.ReportObjects["Text42"]).Text = month1.ToString();

            ((TextObject)report.ReportDefinition.ReportObjects["Text41"]).Text = day1.ToString();

 

            //下面开始动态加载SQL脚本,此次是通过dataset加载水晶报表

            DataSet Ds = db.GetDataSet("select snumber,sname,Grade,Grade_ps,Grade_end,Grade_design from sc where cno=" + Convert.ToString(cno) + " and bno=" + Convert.ToString(bno));

            report.SetDataSource(Ds.Tables[0]);

            //最后绑定数据源,不写无法显示哦^o^

            this.crystalReportViewer1.ReportSource = report;

        }

    }

}

 

u 实验步骤4

下面分别介绍上面主程序部分设计的业务逻辑层方法的具体代码,由于这些方法都是引用了数据库存储过程部分,故而同时将展示存储过程开发的代码部分。
未完待续————————