首页 | 互联网 | IT动态 | IT培训 | Cisco | Windows | Linux | Java | .Net | Oracle | 软件测试 | C/C++ | 嵌入式开发 | 存储世界 | 服务器
网络设备 | IDC | 安全 | 求职招聘 | 数字网校 | 网页设计 | 平面设计 | 技术专题 | 电子书下载 | 教学视频 | 源码下载 | 搜索 | 博客 | 论坛
中国IT实验室Dotnet频道
中国IT教育
Google
首页 ASP.NET  C#  XML/WebService ADO.NET VC.NET VB.NET .NET 资讯动态 专题 RSS订阅 讨论 下载
您现在的位置: 中国IT实验室 >> Dotnet >> ASP.NET >> 正文

使用ASP.NET 2.0中的ReportViewer控件

    介绍

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

    报表示例

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

    上面的报表是一个非常简单的以国家分组的顾客信息列表。 报表的数据是从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 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 list = new List();
   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 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 list = new List();
   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;
  }

 }
}

[1] [2] [3] [4] [5] [6] 下一页

【责编:Ken】

中国IT教育

相关产品和培训
文章评论
 友情推荐链接
 认证培训
 专题推荐

 ·WEB程序开发--ASP.NET和PHP、JSP究竟学哪个?
 ·五步带你入门XML
 ·关于Java框架技术专题
 ·XML全攻略技术专题
 ·JAVA开源技术介绍专题
 ·Java嵌入式开发之J2ME技术专题
 ·超前体验 Oracle 11g的5个新特性…
 ·揭密使用VB.NET的五个实用技巧
 ·Oracle和SQL Server常用函数对比专题…
 ·展现C#世界 C#程序设计专题…
 今日更新
 社区讨论
 博客论点
 频道精选
 Dotnet频道相关导航