c# - 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);
}
解决方案
对我来说,我传递了 .rdl 文件中提到的错误数据集名称。例如
<DataSet Name="TestAbc"> </DataSet>
从我添加的模型
new ReportDataSource("Abc", listOfModel),
您必须传递您在 .rdl 文件中提到的相同名称,并将其作为数据源名称传递。
推荐阅读
- c - 在链表的第 n 个节点中添加值
- android - 在Android中加载广告之前的IMA SDK黑屏
- apache-kafka - 某个时间间隔内来自 kafka 主题的唯一消息
- c++ - 选择排序有什么问题?
- anaconda - Pycharm 终端中没有名为“conda”的模块
- spring-integration - 如何使用spring集成将邮件存储到数组列表对象或数据库中
- python - Python Kivy 2 Buttons 2 不同的动作在相同的布局
- python - 使用谷歌翻译使用 selenium 进行网页抓取
- asp.net-mvc - 在 Entity Framework Core 中使用 FromSqlRaw 时如何编写连接语句?
- amazon-web-services - 将 ETL 作业用于 aws 胶水时,如何控制 RDS 的摄取率?