在 Visual C# 2015 中运行时更改 MySql 数据集查询

Change MySql DataSet query at runtime in Visual C# 2015

在 Visual C# 2015 桌面项目中,我创建了一个从 MySql table 检索数据的数据集。我还创建了一个报告,并且能够在其中添加 table 的三个字段。然后我创建了一个表单,添加了一个 ReportViewer 并选择了前面提到的 Report。

报表看起来不错,并按预期加载了数据。但是,我希望能够以编程方式修改提供报告的 ​​DataSet TableAdapter 的 sql 查询(或任何其他可以实现相同目的的对象)。我在其属性中看到了 SelectCommand -> CommandText 属性(这是我想在运行时更改的那个)。 Visual C# 不允许;所以我尝试对查询 "Fill, GetData()" CommandText 属性 做同样的事情,但我也做不到。

在表单加载事件中我有这个(默认没有修改):

this.productosTableAdapter.Fill(this.dSProductos.productos);
this.reportViewer1.RefreshReport();

"productosTableAdapter" 是我的 TableAdapter,"dsProductos.productos" 是 DataTable。当然,我尝试了上面提到的方法但无济于事(例如:this.productosTableAdapter.Adapter.SelectCommand.CommandText = "select field1, field2 from...")。

我可以做些什么来更改在运行时为我的报告提供数据集的 sql 查询? (请注意,我使用的是 MySQL,而不是 SQL 服务器)。

问题是您使用的是 typed 数据集,它有一个 table 预定义的列列表。

为报表改用常规 DataTable。我假设您的 reportViewer1 有一个 DataSource 属性 预定义到您键入的数据集,所以这就是我在查看器上添加 DataSource 属性 的原因。

System.Data.DataTable tbl = new System.Data.DataTable();
this.productosTableAdapter.Fill(tbl);
this.reportViewer1.DataSource = tbl;
this.reportViewer1.RefreshReport();

最后,我能够在运行时更改 table 适配器查询的 SQL。 即使通过 table 适配器创建多个查询是一种解决方法,也不是我想要的。

我将分享我的解决方案,以便它可以帮助任何有相同用例的人。

在研究了很多文章之后,我首先看的是数据集设计器生成的代码并搜索 table 适配器的声明,这样我就可以扩展它并添加一个可以修改的方法查询的sql。我发现了以下内容:

namespace pos.source.datasets.ProductosTableAdapters {

    /// <summary>
    ///Represents the connection and commands used to retrieve and save data.
    ///</summary>
    [global::System.ComponentModel.DesignerCategoryAttribute("code")]
    [global::System.ComponentModel.ToolboxItem(true)]
    [global::System.ComponentModel.DataObjectAttribute(true)]
    [global::System.ComponentModel.DesignerAttribute("Microsoft.VSDesigner.DataSource.Design.TableAdapterDesigner, Microsoft.VSDesigner" +
        ", Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
    [global::System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter")]
    public partial class productosTableAdapter : global::System.ComponentModel.Component {
        private global::MySql.Data.MySqlClient.MySqlDataAdapter _adapter;
        private global::MySql.Data.MySqlClient.MySqlConnection _connection;
        private global::MySql.Data.MySqlClient.MySqlTransaction _transaction;
        private global::MySql.Data.MySqlClient.MySqlCommand[] _commandCollection;
...

所以,基于此,我在单独的文件中添加了一个新的class,内容如下(相同的命名空间和相同的class声明):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace pos.source.datasets.ProductosTableAdapters
{
    public partial class productosTableAdapter : global::System.ComponentModel.Component
    {
        public void SetSQL(string theSQL)
        {
            this.CommandCollection[0].CommandText = theSQL;
        }
    }
}

然后,我修改了负责加载报表的表单中按钮的代码:

sqlQuery = "select * from productos order by codigo";  // Basic sample where I just changed the "order by" clause which will be chosen dynamically on the form by the end user
this.productosTableAdapter.SetSQL(sqlQuery);
this.productosTableAdapter.Fill(this.dSProductos.productos);
this.reportViewer1.RefreshReport();

因此,通过这种方式,我可以在运行时更改第一个 table 适配器查询 sql 命令文本。