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

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

介绍

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

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

报告文件: codetest.frx 文件

bash 复制代码
<?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:&#13;&#10;- go to &quot;Data&quot; menu and select &quot;Choose Report Data...&quot; item to select a datasource;&#13;&#10;- go to &quot;Report|Configure Bands...&quot; menu to create the band structure;&#13;&#10;- return to the report page, doubleclick the data band to show its editor;&#13;&#10;- choose the datasource;&#13;&#10;- 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&gt;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>

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

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

核心代码:

csharp 复制代码
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();

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

完整代码:

csharp 复制代码
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();
        }
    }
}

(扩展)小结一下:

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

答:没错

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

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


FastReport.Utils.DataComponentBase 是 FastReport 报表生成器中的一个类,它是 FastReport 中的数据组件基类。FastReport 是一个用于生成复杂报表的工具,它支持多种数据源,包括数据库、XML、Excel 文件等。
DataComponentBase 类提供了一系列方法,用于处理报表中的数据。这些方法包括:

  • GetData(): 获取数据源中的数据。
  • SetData(): 设置数据源中的数据。
  • Refresh(): 刷新数据源,重新从数据源获取数据。
  • GetRow(): 获取数据源中的指定行。
  • SetRow(): 设置数据源中的指定行的数据。
  • AddRow(): 在数据源中添加新行。
  • DeleteRow(): 删除数据源中的指定行。
    这些方法为报表提供了基本的数据操作功能,使得用户可以轻松地处理报表数据。此外,DataComponentBase 类还可能包含其他用于数据处理的方法和属性。
    在使用 FastReport 创建报表时,您可能需要从 DataComponentBase 派生出自己的类,以便为特定的数据源或数据操作提供自定义逻辑。这可以通过继承 DataComponentBase 类并重写其方法来实现。
    请注意,FastReport 的具体类和方法可能会随着版本的更新而变化,因此建议查阅最新的 FastReport 文档以获取最准确的信息。
相关推荐
泡干脆面就番茄2 小时前
MySQL_子查询_分页查询与联合查询详解
数据库·mysql
devpotato3 小时前
缓存与数据库更新顺序不一致问题
java·数据库·redis
天若有情6733 小时前
Node+MySQL小型全栈笔记项目实战课程分享
数据库·笔记·mysql
wxwx_bscxy3223 小时前
基于springboot宠物领养系统的设计与实现
数据库·spring boot·后端·spring·宠物
九月要晚安4 小时前
达梦DW与AFC对比
数据库
实战派K8S&DB4 小时前
如何在内网配置 TiDB 数据库 Agent
数据库·人工智能·分布式·tidb
实战派K8S&DB4 小时前
《基于 Dify + FastAPI + PyTiDB 搭建大模型驱动的 TiDB 智能运维 Agent》
运维·数据库·分布式·云原生·tidb·fastapi
SelectDB4 小时前
Apache Doris 5.0 年度版本前瞻(一):构建统一的多模湖仓实时分析平台
大数据·数据库·数据分析
小白学大数据5 小时前
超简单:用 Python 让 Excel 飞起来:用 openpyxl 把重复报表整理交给脚本
开发语言·数据库·python·excel
西索斯coding5 小时前
doubao-seed-2.1-turbo 调用一直 401 怎么办?pro 版同样的 Key 却正常——5 分钟排查定位指南
java·服务器·数据库·ai