如何将数据表传递给SSRS报告查看器?

时间:2015-03-18 07:57:19

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

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

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

1 个答案:

答案 0 :(得分:0)

我用

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

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

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