如何使用.NET从SSRS报告中获取数据源信息

我目前正在制作一个ASP.Net和C#页面,这是一些报告的前端。

我还想从与报告相同的数据源运行一些查询(每个报告只使用一个数据源)。

是否可以使用ReportingService2005或ReportExecutionService成员从报表中提取数据源连接信息,以便可以在SqlConnection中重用它?

您可以使用ReportingService2005 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]; 

上面代码中的ds是DataSourceDefinition或DataSourceReference 。 如果它是一个定义,您可以将其转换为该类型,然后使用以下代码获取连接字符串。

 DataSourceDefinition dsd = ds as DataSourceDefinition(); if(dsd == null) throw new Exception(); String connectionString = dsd.ConnectString; 

如果是数据源参考,则需要查看API 。

希望这有一些帮助。 它将列出所有属性,但具体来说它会拉出连接字符串:

 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("=============================="); } } }