【问题标题】:How to Pass Data Table to SSRS Report Viewer?如何将数据表传递给 SSRS 报表查看器?
【发布时间】:2015-05-20 22:10:33
【问题描述】:
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);
}

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

我应该如何将数据表传递给报表查看器?试图将其更改为数据集,但这会引发使用数据集执行存储过程的错误我将如何使用数据表传递..?

【问题讨论】:

    标签: sql asp.net reporting-services datatable ssrs-2008


    【解决方案1】:

    我用

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

    其中 strDSN 是报告中数据集的名称,ReportData 是包含报告数据的数据表。因此,对于报告中的每个数据集,您需要添加匹配的对报告数据集名称和包含该数据集数据的数据表。数据集名称必须与报告定义中的一个匹配。数据表中的列必须与报表数据集中的列相匹配。

    对于本地报告,您必须提供要在报告中显示的数据。这是本地报表 (.RDLC) 和服务器端报表 (.RDL) 之间的主要区别。事实上,您可以使用 Visual Studio 将所有报告编写为服务器端报告,并通过为它们提供数据来简单地将它们作为本地报告运行。另一个关键区别是服务器端报告允许您使用动态参数,而对于本地报告,这些参数必须是静态的,但您只需在运行时为它们提供参数值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多