Как получить информацию об источнике данных из отчета SSRS с помощью .NET
В настоящее время я создаю страницу ASP.Net и C#, которая является интерфейсом для некоторых отчетов.
Я также хочу выполнить несколько запросов из того же источника данных, что и отчеты (каждый отчет использует только 1 источник данных).
Можно ли извлечь информацию о подключении к источнику данных из отчета с помощью членов ReportingService2005 или ReportExecutionService, чтобы их можно было повторно использовать в SqlConnection?
Ответов (2)2
Вы можете использовать API ReportingService2005, чтобы получить источник данных, используемый конкретным отчетом.
Вам нужен полный путь к отчету (который, как я полагаю, у вас есть), а затем используйте его для запроса службы отчетов о ее источнике данных ( 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("==============================");
}
}
}