wpf 如何在rdlc c#报告中使用多个数据集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32262272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use multiple datasets in rdlc c# report
提问by Wesley Heron
Firstly I need to create reports that use a select from database through TextBoxinput. How can I get textbox value and generate a report based in this TextBox? And if I need to use multiple DataSetsfor fill some tables with informations in my report, how to do it? Note: I use a WPF to get TextBoxesvalues and Winforms to create reportViewer.
首先,我需要创建使用从数据库中通过TextBox输入进行选择的报告。如何获取文本框值并基于此生成报告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();
}
采纳答案by DRapp
You might need to clarify multiple DataSets??? or Multiple tables that are WITHIN a single DataSet.
您可能需要澄清多个数据集???或在单个数据集中的多个表。
Using a SQL Data Adapter, you can run fill on a single DataTable instead of a DataSet, then add the table to a main dataset, and finally provide THAT dataset to the report.
使用 SQL 数据适配器,您可以对单个数据表而不是数据集运行填充,然后将该表添加到主数据集,最后将那个数据集提供给报表。
Just as an example...
举个例子...
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 );
Now, you can obviously change whatever queries you need to get data from the source database, but then put them all into a single DataSet and they should be available for your report to run through.
现在,您显然可以更改从源数据库获取数据所需的任何查询,然后将它们全部放入单个 DataSet 中,它们应该可供您的报表运行。
回答by Calvinthesneak
I'm using multiple dataSources for my reports. I embed a ReportViewer on a winform and add buttons/textBox/comboBox to pass the report parameters. Don't mind example is in VB.NET, but it's doing the exact same thing. I would pass the text box value as a parameter to your query after running sanity checks to ensure you're not trying to pass a NULL or invalid result(See below where I declare my ComboBox Value as an Integer and perform a conversion just to be sure).
我正在为我的报告使用多个数据源。我在 winform 上嵌入了一个 ReportViewer 并添加了按钮/文本框/组合框来传递报告参数。不要介意示例在 VB.NET 中,但它正在做完全相同的事情。在运行健全性检查后,我会将文本框值作为参数传递给您的查询,以确保您不会尝试传递 NULL 或无效结果(请参阅下面我将 ComboBox 值声明为整数并执行转换的位置)当然)。
Generally I load ComboBox's with database values, and have some dateTimePickers, and then a button to execute the report once parameters are selected.
通常,我使用数据库值加载 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
Let me know if you need this clarified.
如果您需要澄清这一点,请告诉我。
回答by Sarath Kumar
Just rewrite the SQL query based on the input from text box value.
只需根据来自文本框值的输入重写 SQL 查询。
_adp = new SqlDataAdapter("Select * from tbl_cad WHERE columnName='"+textBox1.value+"'",_con);

