2015-08-28 30 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 

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

相關問題