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 文档以获取最准确的信息。
相关推荐
TDengine (老段)5 小时前
TDengine 数学函数 DEGRESS 用户手册
大数据·数据库·sql·物联网·时序数据库·iot·tdengine
TDengine (老段)5 小时前
TDengine 数学函数 GREATEST 用户手册
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据
安当加密6 小时前
云原生时代的数据库字段加密:在微服务与 Kubernetes 中实现合规与敏捷的统一
数据库·微服务·云原生
爱喝白开水a6 小时前
LangChain 基础系列之 Prompt 工程详解:从设计原理到实战模板_langchain prompt
开发语言·数据库·人工智能·python·langchain·prompt·知识图谱
想ai抽6 小时前
深入starrocks-多列联合统计一致性探查与策略(YY一下)
java·数据库·数据仓库
武子康6 小时前
Java-152 深入浅出 MongoDB 索引详解 从 MongoDB B-树 到 MySQL B+树 索引机制、数据结构与应用场景的全面对比分析
java·开发语言·数据库·sql·mongodb·性能优化·nosql
longgyy6 小时前
5 分钟用火山引擎 DeepSeek 调用大模型生成小红书文案
java·数据库·火山引擎
ytttr8737 小时前
C# 仿QQ聊天功能实现 (SQL Server数据库)
数据库·oracle·c#
盒马coding8 小时前
第18节-索引-Partial-Indexes
数据库·postgresql
不剪发的Tony老师8 小时前
CloudDM:一站式数据库开发管理工具
数据库