,c#,xml,reporting-services,rdlc"/>

首页 > 解决方案 > RDLC XML 数据源给出错误:“无法为数据集创建数据读取器"

问题描述

我正在尝试在 C# 中构建一个方法来让 SSRS Reportviewer 生成 PDF 输出。

有很多关于这个主题的信息,所以这似乎是一件容易的事。

当我的 DataSource 来自 XML URL 时,就会出现问题。

这是我的方法:

private void GetPDF(Stream RDL)
{
    Warning[] warnings = null;
    string[] streamIds = null;
    string mimeType = string.Empty;
    string encoding = string.Empty;
    string extension = string.Empty;
    string filetype = string.Empty;

    ReportViewer RPT = new ReportViewer();
    RPT.ProcessingMode = ProcessingMode.Local;
    RPT.LocalReport.LoadReportDefinition(RDL);
    RPT.LocalReport.Refresh();

    byte[] Bytes = RPT.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);

    using (FileStream PDF = new FileStream("test.PDF", FileMode.Create))
    {
        PDF.Write(Bytes, 0, Bytes.Length);
    } 
}

该报告在 ReportBuilder 桌面应用程序中运行良好。如您所见,我删除了所有动态内容(报告 URL、参数等)以简化和检测问题来源。

无奈之下,我还尝试将 XML 数据嵌入到报告中。

错误总是一样的:

无法为数据集创建数据读取器(报表上第一个数据集的名称)

数据集查询如下所示:

<Query>
<ElementPath IgnoreNamespaces='true'>
Object1 {
    Element1,
    Element2
}
</ElementPath>
</Query>

请帮忙!!

解决方案:

我解决了这个问题,我想分享我的解决方案,这样你就不会在同样的问题上投入太多时间。

该问题是由报表查看器控件产生的,因为它没有“查询”功能。

然后,我的解决方案是使用报告中所需的确切数据构建一个数据集。

XML 由 te C# Class 下载。数据被收集、过滤并作为数据集发送到 RDL。

最终的方法是这样的:

private MemoryStream RenderPDFFromXML(Stream RDL, Stream XML)
{
    Warning[] warnings = null;
    string[] streamIds = null;
    string mimeType = string.Empty;
    string encoding = string.Empty;
    string extension = string.Empty;
    string filetype = string.Empty;

    ReportViewer RPT = new ReportViewer();
    RPT.ProcessingMode = ProcessingMode.Local;
    RPT.LocalReport.LoadReportDefinition(RDL);

    DataSet DS = new DataSet();
    XML.Position = 0;
    DS.ReadXml(XML);    
    ReportDataSource RDS;
    foreach (DataTable DT in DS.Tables)
    {
        RDS = new ReportDataSource(DT.TableName, DT);
        RPT.LocalReport.DataSources.Add(RDS);
    }
    RPT.LocalReport.Refresh();
    byte[] Bytes = RPT.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
    return new MemoryStream(Bytes);
}

标签: c#xmlreporting-servicesrdlc

解决方案


对我来说,我传递了 .rdl 文件中提到的错误数据集名称。例如

<DataSet Name="TestAbc"> </DataSet>

从我添加的模型

new ReportDataSource("Abc", listOfModel),

您必须传递您在 .rdl 文件中提到的相同名称,并将其作为数据源名称传递。


推荐阅读