繁体   English   中英

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

[英]How to get the data source information from a SSRS report, using .NET

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

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

是否可以使用 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 是DataSourceDefinitionDataSourceReference 如果它是一个定义,您可以将其转换为该类型,然后使用以下代码获取连接字符串。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM