显示嵌入在DLL文件中的.RDLC报告

Jim*_*Jim 39 rdlc embedded-resource reporting-services

我有一个由Windows服务和表单应用程序使用的报告.所以,我想把报告嵌入一个可供两者使用的DLL文件中.

问题是,如果我尝试在我的Windows窗体应用程序中设置ReportViewer控件的ReportEmbeddedResource属性,它将在Windows窗体应用程序中搜索资源,而不是dll文件.

例如:来自Windows窗体应用程序的代码:

rv.LocalReport.ReportEmbeddedResource = "MyReportInMyDLLFile.rdlc"
Run Code Online (Sandbox Code Playgroud)

如何让上面的命令查找我的DLL文件中的嵌入式资源?

gsc*_*ger 55

这样的事情应该这样做:

Assembly assembly = Assembly.LoadFrom("Reports.dll");
Stream stream = assembly.GetManifestResourceStream("Reports.MyReport.rdlc");
reportViewer.LocalReport.LoadReportDefinition(stream);
Run Code Online (Sandbox Code Playgroud)

  • 我爱你. (8认同)
  • 当我在 SharePoint 2010 的“应用程序页面”中使用上述代码时,出现以下错误:“无法加载文件或程序集“file:///c:\windows\system32\inetsrv\Reports.dll”或其依赖项之一。系统找不到指定的文件。`,但它在 WinForm 应用程序中工作。 (2认同)

Dan*_*ham 23

只需使用程序集的完整命名空间,然后使用文件夹名称,然后使用文件名:

rv.LocalReport.ReportEmbeddedResource = 
    "My.Assembly.Namespace.Folder1.Folder2.MyReport.rdlc";
Run Code Online (Sandbox Code Playgroud)

然后使用属性窗格确保将报告文件设置为嵌入式资源.

  • 我不太确定,但我想这只有在报告与此代码所在的同一程序集中才有效. (4认同)

DrC*_*mel 11

可能最好的做法是从其他程序集获取RDLC资源的流,然后将其传递给Report Viewer控件的"LoadReportDefinition"方法.

可以在此处找到如何从不同程序集中的嵌入式资源获取流的详细信息:使用ResourceManager类检索资源

此外,您需要使用它的完整命名空间路径来引用嵌入式资源.

例如,如果您的应用程序具有TheApp的默认命名空间,并且在名为" Reports " 的文件夹中保留名为" MyReport.rdlc "的报告,则报告引用调用将为: -

rv.LocalReport.ReportEmbeddedResource = "TheApp.Reports.MyReport.rdlc";
Run Code Online (Sandbox Code Playgroud)