簡體   English   中英

如何將數據表傳遞到SSRS報告查看器?

[英]How to Pass Data Table to SSRS Report Viewer?

using (SqlConnection conn = new SqlConnection(connection))
{
    SqlDataAdapter da = new SqlDataAdapter();
    DataTable ds = new DataTable();

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;

    conn.Open();

    cmd.CommandText = StoredProcedure;
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("@iDomainID", 1));

    foreach (var item in reportParams)
    {
        cmd.Parameters.Add(new SqlParameter("@" + item.ParameterName, "'" + item.FieldValue + "'"));
    }

    da.SelectCommand = cmd;
    da.Fill(ds);

    conn.Close();

    string strFile;
    strFile = "Reports/" + ReportName;

    ReportViewer1.Visible = true;
    System.IO.StreamReader sr = new System.IO.StreamReader(HttpContext.Current.Server.MapPath("/") + strFile);
    string strReportdef = sr.ReadToEnd();
    sr.Close();

    ReportViewer1.LocalReport.EnableExternalImages = true;
    ReportViewer1.LocalReport.ReportPath = HttpContext.Current.Server.MapPath("/") + strFile;

    int iTables = 0;
    System.Collections.Generic.IList<string> ilReportDSs =   ReportViewer1.LocalReport.GetDataSourceNames();

    for (iTables = 0; iTables < ds.Columns.Count; iTables++)
    {
        ReportDataSource rdSource = new ReportDataSource(ilReportDSs[iTables], ds.Columns[iTables]);
        ReportViewer1.LocalReport.DataSources.Add(rdSource);
    }

    ReportViewer1.DataBind();
}    

這會導致錯誤

for (iTables = 0; iTables < ds.Columns.Count; iTables++)
{
    ReportDataSource rdSource = new ReportDataSource(ilReportDSs[iTables], ds.Columns[iTables]);
    ReportViewer1.LocalReport.DataSources.Add(rdSource);
}

For ReportDataSource rdSource = new ReportDataSource(ilReportDSs[iTables], ds.Columns[iTables]);

如何將數據表傳遞給報表查看器? 試圖將其更改為數據集,但是使用數據集執行存儲過程時會引發錯誤,我將如何使用數據表來傳遞數據呢?

我用

              this.CAreports.LocalReport.DataSources.Add(new ReportDataSource(strDSN, ReportData));

其中,strDSN是報表中數據集的名稱,而ReportData是包含報表數據的數據表。 因此,對於報表中的每個數據集,您需要添加一個匹配對報表數據集名稱和一個包含該數據集數據的數據表。 數據集名稱必須與報表定義中的一個匹配。 數據表中的列必須與報告數據集中的列匹配。

對於本地報告,您必須提供要在報告中顯示的數據。 這是本地報告(.RDLC)和服務器端報告(.RDL)之間的主要區別。 實際上,您可以使用Visual Studio將所有報告編寫為服務器端報告,並通過為它們提供數據來簡單地將其作為本地報告運行。 另一個主要區別是服務器端報表允許您使用動態參數,而本地報表必須使用靜態參數,但是您只需在運行時為其提供參數值即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM