如何在 rdlc c# 报告中使用多个数据集

How to use multiple datasets in rdlc c# report

首先,我需要通过 TextBox 输入创建使用来自数据库的 select 的报告。如何获取文本框值并生成基于此 TextBox 的报告?如果我需要使用多个 DataSets 在我的报告中用信息填充一些表格,该怎么做?注意:我使用 WPF 获取 TextBoxes 值并使用 Winforms 创建 reportViewer.

private void Report_Load(object sender, EventArgs e)
{

      DataSet dsr = new DataSet();
      _con = new SqlConnection(_strCon);
      _adp = new SqlDataAdapter("Select * from tbl_cad",_con);
      _adp.Fill(dsr,dsr.Tables[0].TableName);

      ReportDataSource rds = new ReportDataSource("tbl_cad",dsr.Tables[0]);
      this.reportViewer.LocalReport.DataSources.Clear();
      this.reportViewer.LocalReport.DataSources.Add(rds);
      this.reportViewer.LocalReport.Refresh();

      this.reportViewer.RefreshReport();
}

只需根据文本框值的输入重写 SQL 查询即可。

_adp = new SqlDataAdapter("Select * from tbl_cad WHERE columnName='"+textBox1.value+"'",_con);

我正在为我的报告使用多个数据源。我在 winform 上嵌入了一个 ReportViewer 并添加 buttons/textBox/comboBox 以传递报表参数。不要介意示例在 VB.NET 中,但它做的是完全相同的事情。在 运行 健全性检查之后,我会将文本框值作为参数传递给您的查询,以确保您没有尝试传递 NULL 或无效结果(请参见下文,我将 ComboBox 值声明为 Integer 并执行转换只是为了确定)。

通常我用数据库值加载 ComboBox,并有一些 dateTimePickers,然后在选择参数后有一个按钮来执行报告。

Private Sub auditYearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles auditYearButton.Click
Dim year As Integer = CInt(yearComboBox.Text)
weeklyReportViewer.Clear()
weeklyReportViewer.Reset()
weeklyReportViewer.LocalReport.DataSources.Clear()

Dim eYear As New Microsoft.Reporting.WinForms.ReportParameter("year", CStr(year))

Dim itm As New Microsoft.Reporting.WinForms.ReportDataSource
Dim itm2 As New Microsoft.Reporting.WinForms.ReportDataSource
Dim itm3 As New Microsoft.Reporting.WinForms.ReportDataSource
Dim ta As New DsFSCTableAdapters.v_ConvertTableAdapter
Dim tb As New DsFSCTableAdapters.AllocationTableAdapter
Dim tc As New DsFSCTableAdapters.v_ConvertCommercialTableAdapter


weeklyReportViewer.LocalReport.ReportEmbeddedResource = "MERP.AuditYearAllocationReport.rdlc"
Me.weeklyReportViewer.LocalReport.SetParameters(New Microsoft.Reporting.WinForms.ReportParameter() {eYear})

tb.FillByYear(DsFSC.Allocation, year)
itm2.Name = "DsFSC_Allocation"
itm2.Value = AllocationBindingSource


ta.Fill(DsFSC.v_Convert)
itm.Name = "DsFSC_v_Convert"
itm.Value = v_ConvertBindingSource

tc.Fill(DsFSC.v_ConvertCommercial)
itm3.Name = "DsFSC_v_ConvertCommercial"
itm3.Value = v_ConvertCommercialBindingSource

weeklyReportViewer.LocalReport.DataSources.Add(itm)
weeklyReportViewer.LocalReport.DataSources.Add(itm2)
weeklyReportViewer.LocalReport.DataSources.Add(itm3)
Me.weeklyReportViewer.RefreshReport()
End Sub

如果您需要澄清,请告诉我。

您可能需要澄清多个数据集???或单个数据集中的多个 table。

使用 SQL 数据适配器,您可以 运行 填充单个数据表而不是数据集,然后将 table 添加到主数据集,最后提供该数据集报告。

举个例子...

DataSet dsr = new DataSet();
_con = new SqlConnection(_strCon);

_adp = new SqlDataAdapter("Select * from tbl_cad",_con);
DataTable tbl1 = new DataTable();
tbl1.TableName = "TableNameForReport";
_adp.Fill( tbl1 );

_adp = new SqlDataAdapter("Select * from OtherTable",_con);
DataTable tbl2 = new DataTable();
tbl2.TableName = "AnotherTableNameForReport";
_adp.Fill( tbl2 );

dsr.Tables.Add( tbl1 );
dsr.Tables.Add( tbl2 );

现在,您显然可以更改从源数据库获取数据所需的任何查询,然后将它们全部放入一个数据集中,它们应该可用于您的报告 运行 通过。