频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

使用ASP.NET 2.0中的ReportViewer控件

来源:论坛整理 作者:Bipin Joshi 出处:巧巧读书 2007-08-22 进入讨论组
下一页 1 2 3 
本文翻译:webabcd

  介绍

  任何数据驱动型的应用程序都有一个普遍的需求,那就是报表。 但是,在ASP.NET 1.x中并没有给我们提供这个非常重要的特性。 然而很幸运的是,伴随着.NET 2.0而来的ReportViewer控件可以满足你对报表的一些基本需求。 我将会在本文中向你演示如何使用这个控件。 ReportViewer控件既可以在web程序中使用,也可以在windows程序中使用。 在这里,我将只介绍如何在web程序中使用它。

  报表示例

  我们假设要生成一个如下所示的顾客信息列表:

  使用ASP.NET 2.0中的ReportViewer控件(图一)


  上面的报表是一个非常简单的以国家分组的顾客信息列表。 报表的数据是从Northwind数据库的Customers表里获取的。 默认情况下,它会显示所有的顾客信息。 但是,你也可以让它显示属于你指定的某个国家的顾客信息。

  该报表是使用ReportViewer控件设计的,它可以从强类型的DataSet中或者自定义的对象集合中获取数据。 在实际的程序开发中,我们往往会使用3层架构,数据的获取经常会是从业务层取得的DataSet或一个泛型集合。 在这里,我打算使用一个泛型集合作为数据源,而不是强类型的DataSet。

  创建类库

  首先,打开Visual Studio,然后创建一个名为ReportViewerLib的类库项目。 添加一个如下所示的名为Customer的类:

using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Collections.Generic;
namespace ReportViewerLib
{
 public class Customer
 {
  public string strCustomerID;
  public string strCompanyName;
  public string strContactName;
  public string strCountry;

  public string CustomerID
  {
   get
   {
    return strCustomerID;
   }
   set
   {
    strCustomerID = value;
   }
  }

  public string CompanyName
  {
   get
   {
    return strCompanyName;
   }
   set
   {
    strCompanyName= value;
   }
  }

  public string ContactName
  {
   get
   {
    return strContactName;
   }
   set
   {
    strContactName= value;
   }
  }

  public string Country
  {
   get
   {
    return strCountry;
   }
   set
   {
    strCountry= value;
   }
  }

  public static List<Customer> GetCustomersForCountry(string country)
  {
   SqlConnection cnn=new SqlConnection(
    ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
   SqlCommand cmd=new SqlCommand();
   cmd.Connection=cnn;
   cmd.CommandText="select
   CustomerID,CompanyName,ContactName,Country
from customers where country=@country";
   SqlParameter p=new SqlParameter
("@country",country);
   cmd.Parameters.Add(p);
   cnn.Open();
   SqlDataReader reader = cmd.ExecuteReader();
   List<Customer> list = new List<Customer>();
   while (reader.Read())
   {
    Customer c = new Customer();
    c.CustomerID = reader.GetString(0);
    c.CompanyName = reader.GetString(1);
    c.ContactName = reader.GetString(2);
    c.Country = reader.GetString(3);
    list.Add(c);
   }
   cnn.Close();
   return list;
  }

  public static List<Customer> GetAllCustomers()
  {
   SqlConnection cnn = new SqlConnection(
    ConfigurationManager.ConnectionStrings
    ["NorthwindConnectionString"].ConnectionString);
   SqlCommand cmd = new SqlCommand();
   cmd.Connection = cnn;
   cmd.CommandText = "select
   CustomerID,CompanyName,ContactName,Country from customers";
   cnn.Open();
   SqlDataReader reader = cmd.ExecuteReader();
   List<Customer> list = new List<Customer>();
   while (reader.Read())
   {
    Customer c = new Customer();
    c.CustomerID = reader.GetString(0);
    c.CompanyName = reader.GetString(1);
    c.ContactName = reader.GetString(2);
    c.Country = reader.GetString(3);
    list.Add(c);
   }
   cnn.Close();
   return list;
  }

 }
}

  Customer类定义了四个公共属性,即CustomerID、CompanyName、ContactName和Country。 在之后,是这个类包含的两个静态方法 – GetCustomersForContry()和GetAllCustomers()。 这两个方法都是比较简单的,一个是返回属于某一个国家的所有顾客信息,另一个是返回全部顾客信息。 首先打开Northwind数据库的连接,然后通过SqlCommand对象执行SELECT查询。 之后,用SqlDataReader对象来获取数据。 遍历这个SqlDataReader对象,在其内每次都创建一个Customer对象,然后设置它的各个属性,最后把其添加到Customer对象的泛型集合中。 在类的结尾处就是把这个Customer对象的泛型集合返回给调用者。URL查看 http://www.qqread.com/aspdotnet/u327510.html 更多文章 更多内容请看.NET移动与嵌入式技术.NET开发手册ASP.NET教程专题,或进入讨论组讨论。
下一页 1 2 3 
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章