FastReport 指定sql,修改数据源,非DataSet修改
  • 介绍
  • 报告文件: codetest.frx 文件
  • 核心代码:
  • (扩展)小结一下:

介绍

FastReport中,经常会遇到需要给 sql 加条件的情况。

(废话不多说)比如下面这个案例。

报告文件: codetest.frx 文件
<?xml version="1.0" encoding="utf-8"?>
<Report ScriptLanguage="CSharp" TextQuality="Regular" ReportInfo.Name="Simple List" ReportInfo.Author="Fast Reports Inc" ReportInfo.Description="Demonstrates a simple list report. To create it:
- go to "Data" menu and select "Choose Report Data..." item to select a datasource;
- go to "Report|Configure Bands..." menu to create the band structure;
- return to the report page, doubleclick the data band to show its editor;
- choose the datasource;
- drag data from the Data Dictionary window to the band." ReportInfo.Created="01/17/2008 03:05:57" ReportInfo.Modified="07/08/2024 17:08:12" ReportInfo.CreatorVersion="2022.3.9.0">
  <Dictionary>
    <MsSqlDataConnection Name="sqlconnection" ConnectionString="Server=.;Database=codepl;User Id=sa;Password=123456;">
      <TableDataSource Name="tmpsn" Alias="mb_table_alias" DataType="System.Int32" Enabled="true" TableName="tmpsn">
        <Column Name="id" DataType="System.Int32"/>
        <Column Name="sn" DataType="System.String"/>
      </TableDataSource>
      <TableDataSource Name="Table" Alias="mb-qtable-alias" DataType="System.Int32" Enabled="true" SelectCommand="select sn from tmpsn where id>1">
        <Column Name="sn" DataType="System.String"/>
      </TableDataSource>
    </MsSqlDataConnection>
  </Dictionary>
  <ReportPage Name="Page1" PaperWidth="64" PaperHeight="32" LeftMargin="1" TopMargin="1" RightMargin="1" BottomMargin="1" MirrorMargins="true" Watermark.Font="宋体, 60pt">
    <DataBand Name="Data2" Width="117.18" Height="27.02" CanGrow="true" KeepChild="true" DataSource="Table" Columns.Count="2" Columns.MinRowCount="4">
      <BarcodeObject Name="Barcode1" Left="3.7" Width="110.31" Height="15.64" AutoSize="false" DataColumn="mb_table_alias.sn" ShowText="false" Barcode="Code128" Barcode.AutoEncode="true"/>
      <TextObject Name="Text1" Left="11.89" Top="16.47" Width="94.5" Height="10.55" Text="[mb-qtable-alias.sn]" Font="宋体, 5pt"/>
    </DataBand>
  </ReportPage>
</Report>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.

FastReport 指定sql,修改数据源 ( 非DataSet修改 )_数据源

如果直接运行的话,就会查询出数据库中的全部:

FastReport 指定sql,修改数据源 ( 非DataSet修改 )_数据源_02

但是我们现在要通过改变Sql来查询,所以需要调整sql语句,可以通过下面代码来改变:

核心代码:
FastReport.Report report = new FastReport.Report();
// 加载 FastReport 的报表文件,该文件包含了报表的布局和数据源配置
report.Load(@"C:\Users\daoli\Desktop\fp-test\codetest.frx");
// 查找报表字典中名为 "mb-qtable-alias" 的数据源,数据源是报表中定义的数据连接
var dataSource = report.Dictionary.FindByAlias("mb-qtable-alias");
if (dataSource is TableDataSource tableDataSource)
{
    // 判断 dataSource 是否是 TableDataSource 类型的实例
    // 如果是,则修改它的 SelectCommand 属性来改变查询命令
    tableDataSource.SelectCommand = "SELECT sn FROM tmpsn WHERE id < 10";
}
// 准备报表,这一步会执行数据源的查询,准备显示报表
report.Prepare();
// 显示已经准备好但尚未打印的报表
report.ShowPrepared();
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

通过核心代码修改Sql之后,就可以实现下图效果了。

FastReport 指定sql,修改数据源 ( 非DataSet修改 )_System_03

FastReport 指定sql,修改数据源 ( 非DataSet修改 )_数据源_04

完整代码:

using FastReport;
using FastReport.Data;
using FastReport.Utils;
using System.Collections;
using System.Data;

using System.Data.SqlClient;

namespace fp_test
{
    public partial class Form1 : Form
    {
         
        public static string sqlConStr = @"Server=.;Database=codepl;User Id=sa;Password=123456;";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FastReport.Report report = new FastReport.Report();
            // 加载 FastReport 的报表文件,该文件包含了报表的布局和数据源配置
            report.Load(@"C:\Users\daoli\Desktop\fp-test\codetest.frx");
            // 查找报表字典中名为 "mb-qtable-alias" 的数据源,数据源是报表中定义的数据连接
            var dataSource = report.Dictionary.FindByAlias("mb-qtable-alias");
            if (dataSource is TableDataSource tableDataSource)
            {
                // 判断 dataSource 是否是 TableDataSource 类型的实例
                // 如果是,则修改它的 SelectCommand 属性来改变查询命令
                tableDataSource.SelectCommand = "SELECT sn FROM tmpsn WHERE id < 10";
            }
            // 准备报表,这一步会执行数据源的查询,准备显示报表
            report.Prepare();
            // 显示已经准备好但尚未打印的报表
            report.ShowPrepared();
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
(扩展)小结一下:

通过 tableDataSource 来设置了Sql,是不是也代表其他地方也可以设置呢。

答:没错

在这里,其实还有很多方法可以实现。我们可以通过配置字典来观察等等。

着一些都是可以通过 DataComponentBase 来接触底层的一些代码。如下图和介绍

FastReport 指定sql,修改数据源 ( 非DataSet修改 )_sql_05

FastReport.Utils.DataComponentBase 是 FastReport 报表生成器中的一个类,它是 FastReport 中的数据组件基类。FastReport 是一个用于生成复杂报表的工具,它支持多种数据源,包括数据库、XML、Excel 文件等。

DataComponentBase 类提供了一系列方法,用于处理报表中的数据。这些方法包括:

  • GetData(): 获取数据源中的数据。
  • SetData(): 设置数据源中的数据。
  • Refresh(): 刷新数据源,重新从数据源获取数据。
  • GetRow(): 获取数据源中的指定行。
  • SetRow(): 设置数据源中的指定行的数据。
  • AddRow(): 在数据源中添加新行。
  • DeleteRow(): 删除数据源中的指定行。
    这些方法为报表提供了基本的数据操作功能,使得用户可以轻松地处理报表数据。此外,DataComponentBase 类还可能包含其他用于数据处理的方法和属性。
    在使用 FastReport 创建报表时,您可能需要从 DataComponentBase 派生出自己的类,以便为特定的数据源或数据操作提供自定义逻辑。这可以通过继承 DataComponentBase 类并重写其方法来实现。
    请注意,FastReport 的具体类和方法可能会随着版本的更新而变化,因此建议查阅最新的 FastReport 文档以获取最准确的信息。