2009-02-26 76 views
4

我目前正在製作一個ASP.Net和C#頁面,這是一些報告的前端。如何從SSRS報告中獲取數據源信息,使用.NET

我也想運行一些查詢,從相同的數據源作爲報告(每個報告只使用1個數據源)。

是否可以使用ReportingService2005或ReportExecutionService成員從報告中提取數據源連接信息,以便它可以在SqlConnection中重用?

回答

3

您可以使用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可以是一個DataSourceDefinitionDataSourceReference。如果它是一個定義,您可以將其轉換爲該類型,然後使用以下代碼獲取連接字符串。

DataSourceDefinition dsd = ds as DataSourceDefinition(); 
if(dsd == null) 
    throw new Exception(); 

String connectionString = dsd.ConnectString; 

如果它是數據源參考,您需要查看API

+0

VB的有點生疏? OP指定C#作爲他們正在使用的語言 – csjohnst 2009-02-27 00:39:18

0

希望這有些幫助。它會列出所有的屬性,但具體它拉出連接字符串:

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