在 C# 中访问 SRSS 数据
Access SRSS data in c#
我有一份 link 的 SRSS 报告,看起来像这样 https://enterprisereports.contoso.com/NA_REPORTS/report/HR/HR%20Department/Open/Roster
。当我在浏览器中打开报告时:
我正在尝试编写 C# 代码以自动从此报告中提取数据。现在,我通过按保存图标手动导出 excel 中的数据,并将其用作我的 c# 应用程序的输入,但我想自动执行此操作。
我尝试过的事情:
- 像这样
https://server/reportserver/ReportService2010.asmx?wsdl
(source) 使用 URL 访问 WSDL 以添加为服务 link。我试过 https://enterprisereports.contoso.com/NA_REPORTS/report/HR/HR%20Department/Open/Roster/ReportService2010.asmx?wsdl
、https://enterprisereports.contoso.com/NA_REPORTS/report/ReportService2010.asmx?wsdl
、https://enterprisereports.contoso.com/NA_REPORTS/report/ReportService2010.asmx?wsdl
、https://enterprisereports.contoso.com/NA_REPORTS/ReportService2010.asmx?wsdl
和 https://enterprisereports.contoso.com/ReportService2010.asmx?wsdl
- 我得到的只是 404 或找不到该项目。
- 使用
https://<Report Server Name>/reportserver?/Sales/YearlySalesSummary&rs:Format=Excel&rs:Command=Render
(source) 通过 url 导出报告。我试过了 https://enterprisereports.contoso.com/NA_REPORTS/report/HR/HR%20Department/Open/Roster&rs:Format=Excel&rs:Command=Render
。我明白了
The path of the item /HR/HR Department/Open/Roster&rs:Format=Excel&rs:Command=Render' is not valid. The
full path must be less than 260 characters long; other restrictions
apply. If the report server is in native mode, the path must start
with slash. (rsInvalidItemPath) Get Online Help
是否有从报告中提取数据的简便方法?
注意 1:我是 SRSS 新手。
注意 2:我知道该报告使用存储在某处(可能是 sql 实例)的数据,但我所拥有的只是报告的 link。获得内部访问权限对我来说不是一个选择:1)不知道该问谁 2)将是一个漫长的过程 3)不确定是否会授予访问权限 :(.
“ReportService2010”服务用于管理,而不用于呈现。
您需要“ReportExecution2005”服务。
报告的 link 大概是这样的:
https://some_server_name.internal/Reports/Foldername/Reportname
然后您应该能够在
找到报告执行服务
https://some_server_name.internal/reportserver/ReportExecution2005.asmx?wsdl
我在 visual studio 中使用 网络参考 而不是服务参考和以下代码来实现此功能。
var rs = new ReportExecutionService();
rs.Url = "https://some_server_name.internal/reportserver/ReportExecution2005.asmx?wsdl";
rs.UseDefaultCredentials = false;
rs.Credentials = new System.Net.NetworkCredential(serviceUser, servicePassword, serviceDomain);
var execInfo = rs.LoadReport("/Foldername/Reportname", HistoryID: null);
// Set any data source credentials and parameters here
rs.SetExecutionCredentials(new[] {
new DataSourceCredentials() {
DataSourceName = datasourcename,
UserName = datasourceUser,
Password = datasourePassword
}
});
// Now, render to XML
var bytes = rs.Render("XML", DeviceInfo: null, out var ext, out var mimetype, out var encoding, out var warnings, out var streamids);
// The bytes will probably contain a BOM,
// so wrap in a memory stream and hand to parser,
// instead of going via a string.
var buffer = new MemoryStream(bytes);
var doc = XDocument.Load(buffer);
// Now you can extract the data you want from the XML.
// For now, maybe save it?
doc.Save("report-result.xml");
可能可以通过“服务参考”来实现这一点,但我在身份验证方面遇到了困难。
另外请注意,XML 将遵循报告中数据集的结构。因此它看起来可能与您预期的不完全一样,因为不会应用报告布局中的 layout/filtering。
我有一份 link 的 SRSS 报告,看起来像这样 https://enterprisereports.contoso.com/NA_REPORTS/report/HR/HR%20Department/Open/Roster
。当我在浏览器中打开报告时:
我正在尝试编写 C# 代码以自动从此报告中提取数据。现在,我通过按保存图标手动导出 excel 中的数据,并将其用作我的 c# 应用程序的输入,但我想自动执行此操作。 我尝试过的事情:
- 像这样
https://server/reportserver/ReportService2010.asmx?wsdl
(source) 使用 URL 访问 WSDL 以添加为服务 link。我试过https://enterprisereports.contoso.com/NA_REPORTS/report/HR/HR%20Department/Open/Roster/ReportService2010.asmx?wsdl
、https://enterprisereports.contoso.com/NA_REPORTS/report/ReportService2010.asmx?wsdl
、https://enterprisereports.contoso.com/NA_REPORTS/report/ReportService2010.asmx?wsdl
、https://enterprisereports.contoso.com/NA_REPORTS/ReportService2010.asmx?wsdl
和https://enterprisereports.contoso.com/ReportService2010.asmx?wsdl
- 我得到的只是 404 或找不到该项目。 - 使用
https://<Report Server Name>/reportserver?/Sales/YearlySalesSummary&rs:Format=Excel&rs:Command=Render
(source) 通过 url 导出报告。我试过了https://enterprisereports.contoso.com/NA_REPORTS/report/HR/HR%20Department/Open/Roster&rs:Format=Excel&rs:Command=Render
。我明白了
The path of the item /HR/HR Department/Open/Roster&rs:Format=Excel&rs:Command=Render' is not valid. The full path must be less than 260 characters long; other restrictions apply. If the report server is in native mode, the path must start with slash. (rsInvalidItemPath) Get Online Help
是否有从报告中提取数据的简便方法?
注意 1:我是 SRSS 新手。
注意 2:我知道该报告使用存储在某处(可能是 sql 实例)的数据,但我所拥有的只是报告的 link。获得内部访问权限对我来说不是一个选择:1)不知道该问谁 2)将是一个漫长的过程 3)不确定是否会授予访问权限 :(.
“ReportService2010”服务用于管理,而不用于呈现。 您需要“ReportExecution2005”服务。
报告的 link 大概是这样的:
https://some_server_name.internal/Reports/Foldername/Reportname
然后您应该能够在
找到报告执行服务https://some_server_name.internal/reportserver/ReportExecution2005.asmx?wsdl
我在 visual studio 中使用 网络参考 而不是服务参考和以下代码来实现此功能。
var rs = new ReportExecutionService();
rs.Url = "https://some_server_name.internal/reportserver/ReportExecution2005.asmx?wsdl";
rs.UseDefaultCredentials = false;
rs.Credentials = new System.Net.NetworkCredential(serviceUser, servicePassword, serviceDomain);
var execInfo = rs.LoadReport("/Foldername/Reportname", HistoryID: null);
// Set any data source credentials and parameters here
rs.SetExecutionCredentials(new[] {
new DataSourceCredentials() {
DataSourceName = datasourcename,
UserName = datasourceUser,
Password = datasourePassword
}
});
// Now, render to XML
var bytes = rs.Render("XML", DeviceInfo: null, out var ext, out var mimetype, out var encoding, out var warnings, out var streamids);
// The bytes will probably contain a BOM,
// so wrap in a memory stream and hand to parser,
// instead of going via a string.
var buffer = new MemoryStream(bytes);
var doc = XDocument.Load(buffer);
// Now you can extract the data you want from the XML.
// For now, maybe save it?
doc.Save("report-result.xml");
可能可以通过“服务参考”来实现这一点,但我在身份验证方面遇到了困难。
另外请注意,XML 将遵循报告中数据集的结构。因此它看起来可能与您预期的不完全一样,因为不会应用报告布局中的 layout/filtering。