在visual c#2015中更改运行时的mysql数据集查询

57hvy0tb  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(239)

在一个visualc#2015桌面项目中,我创建了一个从mysql表检索数据的数据集。我还创建了一个报表,并能够在其中添加表的三个字段。然后我创建了一个表单,添加了一个reportviewer并选择了前面提到的报表。
报告看起来不错,并按预期加载数据。但是,我希望能够以编程方式修改为报表提供数据集tableadapter的sql查询(或任何其他实现相同功能的对象)。我在它的属性中看到selectcommand->commandtext属性(这是我想在运行时更改的属性)。visual c不允许我;因此,我尝试对查询“fill,getdata()”commandtext属性执行相同的操作,但我也做不到。
在form load事件中,我有以下内容(默认值,无需修改):

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,而不是sqlserver)。

ogq8wdun

ogq8wdun1#

问题是您使用的是类型化数据集,它有一个表,其中包含预定义的列列表。
对报表使用常规数据表。我假设您的reportviewer1有一个datasource属性,它是为您的类型化数据集预定义的,所以我在查看器上添加了datasource属性。

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

9lowa7mx2#

最后,我能够在运行时更改表适配器查询的sql。即使通过表适配器创建多个查询是一种解决方法,但这并不是我想要的。
我将分享我的解决方案,以便它可以帮助任何人与我相同的用例。
在研究了很多文章之后,我首先看到的是数据集设计器生成的代码,并搜索表适配器的声明,这样我就可以扩展它并添加一个方法来修改查询的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;
...

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

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();

这样,我就可以在运行时更改第一个表适配器查询sql命令文本。

相关问题