2015-08-28 169 views
2

首先,我需要通过TextBox输入创建使用select from database的报表。如何获得文本框的值并生成基于此TextBox的报表?如果我需要使用多个DataSets来填充报表中的信息,那么该怎么做?注意:我使用WPF获取TextBoxes值,Winforms创建reportViewer如何在rdlc中使用多个数据集c#report

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

回答

1

您可能需要澄清多个数据集???或者在单个DataSet中的多个表。

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

只是作为一个例子...

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

现在,你可以明显改变,你需要从源数据库获取数据,但后来把他们都到一个单一的数据集的任何查询,他们应该是可用让您的报告贯穿始终。

-1

只是基于来自文本框值的输入重写SQL查询。

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

你能闻到SQL注入?即使基于桌面的WPF应用程序,您也应该始终参数化查询。 – DRapp

1

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

通常我加载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 

让我知道你是否需要澄清。

相关问题