[英]How to get the data source information from a SSRS report, using .NET
I am currently making an ASP.Net and C# page, which is a front end for some reports.我目前正在制作一个 ASP.Net 和 C# 页面,这是一些报告的前端。
I also want to run some queries, from the same data source as the reports (each report uses just 1 data source).我还想从与报告相同的数据源运行一些查询(每个报告仅使用 1 个数据源)。
Is it possible to extract the data source connection information from a report, using either ReportingService2005 or ReportExecutionService members, so that it can be reused in an SqlConnection?是否可以使用 ReportingService2005 或 ReportExecutionService 成员从报表中提取数据源连接信息,以便在 SqlConnection 中重用?
You can use the ReportingService2005 API to get the datasource used by a particular report.您可以使用 ReportingService2005 API 获取特定报告使用的数据源。
You need the full path of the report (which I assume you have), and then use it to query the reporting service for its data source ( API ).您需要报告的完整路径(我假设您有),然后使用它来查询报告服务的数据源( API )。
// rs = ReportingService2005 that you need to set up.
DataSource ds;
DataSources dataSources = rs.GetItemDataSources(item);
// item is a string containing the full path to the report.
dataSources = rs.GetItemDataSources(item);
ds = dataSources[0];
The ds in the code above is either a DataSourceDefinition or a DataSourceReference .上面代码中的 ds 是DataSourceDefinition或DataSourceReference 。 If it's a definition you can just cast it into that type and then get the connection string using the following code.如果它是一个定义,您可以将其转换为该类型,然后使用以下代码获取连接字符串。
DataSourceDefinition dsd = ds as DataSourceDefinition();
if(dsd == null)
throw new Exception();
String connectionString = dsd.ConnectString;
If it's a datasourcereference you need to check out the API .如果它是数据源引用,您需要查看API 。
Hopefully this is of some help.希望这会有所帮助。 It will list all the properties, but specifically it pulls out the connection string:它将列出所有属性,但特别是它会拉出连接字符串:
using System;
using GetPropertiesSample.ReportService2010; //This is the WebService Proxy
using System.Diagnostics;
using System.Reflection;
using System.Web.Services.Protocols;
using System.IO;
namespace GetPropertiesSample
{
class Program
{
static void Main(string[] args)
{
Get_Properties_of_DataSource_given_The_Path_and_Name_of_the_Report();
}
private static void Get_Properties_of_DataSource_given_The_Path_and_Name_of_the_Report()
{
try
{
// Create a Web service proxy object and set credentials
ReportingService2010 rs = new ReportingService2010();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
string reportPathAndName = "/0_Contacts/209_Employee_Telephone_List_Printable";
DataSource[] dataSources = rs.GetItemDataSources(reportPathAndName);
DataSource ds = dataSources[0];
string dsName = ds.Name;
Debug.Print("----------------------------------------------------");
Debug.Print("Data Source Name: " + dsName);
Debug.Print("----------------------------------------------------");
DataSourceDefinition dsd = (DataSourceDefinition)ds.Item;
if (dsd != null)
{
//Here is one property
string connectionString = dsd.ConnectString; // <====== HERE is the Connection Strin
//Use Reflection to get all the properties : using System.Reflection;
var typeDSD = typeof(DataSourceDefinition);
var properties = typeDSD.GetProperties();
foreach (PropertyInfo p in properties)
{
Debug.Print("----------------------------------------------------");
Debug.Print(p.Name + ": " + p.GetValue(dsd, null));
}
}
}
catch (SoapException e)
{
Debug.Print("==============================");
Debug.Print(e.Detail.OuterXml);
Debug.Print("==============================");
}
catch (Exception e)
{
Debug.Print("==============================");
Debug.Print(e.Message);
Debug.Print(e.InnerException.ToString());
Debug.Print(e.ToString());
Debug.Print("==============================");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.