Make Reports Quickly With ASP.NET and XML

有时需要快速创建报表,可通过复用ASP.NET网页快速制作存储在XML文件中的报表。下载示例展示了如何在同一页面用三个DataGrids显示不同数据库查询。使用OleDb连接数据源,每个报表有自己的连接字符串和页面大小设置,复用此代码可节省时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[Download Code] | [Run Sample]
Sometimes you need to create reports quickly. You might not always have time to format fancy reports containing graphs. Or maybe you need to create a report that doesn't need graphs. By reusing one ASP.NET web page you can quickly make reports that are stored in XML files. The sample that is included in the download demonstrates how you can display up to three different database queries, using three DataGrids, on the same page. Each report is an XML file that contains a title for the web page and a subtitle, connection string, SQL select statement, and page size for each database query.

Example XML report file:

<?xml version="1.0" encoding="utf-8" ?>
<XmlReport>
  <Page>
    <Title>Products</Title>
  </Page>
  <Report>
    <Title>Product List</Title>
    <ConnectionProvider=Microsoft>.Jet.OLEDB.4.0; Data Source=/products.mdb;</Connection>
    <Sql>Select ProductID, ProductName, UnitPrice From Products;</Sql>
    <PageSize>10</PageSize>
  </Report>
  <Report>
    <Title>Product Summary</Title>
    <ConnectionProvider=Microsoft>.Jet.OLEDB.4.0; Data Source=/products.mdb;</Connection>
    <Sql>Select Count(ProductID) As [Product Count] From Products;</Sql>
    <PageSize></PageSize>
  </Report>
</XmlReport>

The name of the report (XML file) is sent in the QueryString without the file extension (.xml). For example: reports.aspx?report=reportname. The XML report is loaded and the "Reports" table is iterated to bind data to the DataGrids. This would be a perfect place to use a control array, but ASP.NET does not support control arrays. The sample shows a way around this handicap.

OleDb is used to connect to the data sources so you can use any OleDb database. Another neat feature is that each report has its own connection string. This allows you to display reports from different databases on the same page. Each report has a setting for page size; if left blank paging will not be enabled for that report.

The download is a complete working solution that I am currently using in-house. The download contains an Access database and a test report (Test.xml) that displays a product list and a summary. Now, when you want to create a report, just create an XML report file and save it in the reports directory. Then create a link to the reports.aspx page with your new file name (minus the extension) and your report is made. If you create many reports that don't require graphs you will save much time reusing this code sample.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值