zoukankan      html  css  js  c++  java
  • SQL Server Reporting Service(SSRS) 第五篇 自定义数据处理扩展DPE(Data Processing Extension)

          最近在做SSRS项目时,遇到这么一个情形:该项目有多个数据库,每个数据库都在不同的服务器,但每个数据库所拥有的数据库对象(table/view/SPs/functions)都是一模一样的,后来结合网络上众多的资源找到了解决方案,即Data Processing Extensio(DPE)。所谓DPE,直白地说就是开发自己的DLL去扩展SSRS的数据源,具体的操作如下所示:

    1. 新建类库项目,并引入以下两个DLL:

        C:Program FilesMicrosoft SQL ServerMSSQLSERVERReporting ServicesReportServerinMicrosoft.ReportingServices.Interfaces.dll

      C:Program Files (x86)Reference AssembliesMicrosoftFrameworkv3.5System.Data.DataSetExtensions.dll

    2. 根据参数获取Connection String:新建DBConnection类文件,实现接口IDbConnectionExtension并且重写其中的方法:

      A. SetConfiguration():主要是实现读取数据库连接的配置文件:

    public void SetConfiguration(string configuration)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(configuration);
        string ConfigFilePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + xmlDoc.ChildNodes[0].Attributes["configSource"].InnerText;
        ExeConfigurationFileMap file = new ExeConfigurationFileMap();
        file.ExeConfigFilename = ConfigFilePath;
        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
        foreach (ConnectionStringSettings cf in config.ConnectionStrings.ConnectionStrings)
        {
            connectionStrings.Add(cf.Name, cf.ToString());
        }
    }
    View Code

      B. GetConnectionByParameter():主要根据User的选择来返回相应的Database connection;

    public SqlConnection GetConnectionByParameter(IDataParameterCollection parameters)
    {
        var connString = connectionStrings["CentralDBConnString"];
    
        foreach (DBParameter param in parameters)
        {
            if (param.ParameterName.Equals("ClinicCode"))
            {
                //Using the first value if the ClinicCode were selected for more than 1 option.
                if (param.Values != null && param.Values.Length > 1)
                {
                    param.Value = param.Values[0];
                }
                connString = GetConnectionStringsConfig(param.Value.ToString());
            }
        }
    
        return new SqlConnection(connString);
    }
    View Code

    3. 执行SQL命令:新建DBCommand类文件,实现接口IDbCommand并且重写其中的方法:

      A. ExecuteReader(): 执行SQL Command;

    public IDataReader ExecuteReader(CommandBehavior behavior)
    {
        Debug.WriteLine("IDBCommand.ExecuteReader with CommandBehavior." + behavior);
    
        // get connection based on parameters
        using (SqlConnection conn = m_connection.GetConnectionByParameter(this.Parameters))
        {
            conn.Open();
    
            SqlCommand cmd = new SqlCommand(CommandText, conn);
            cmd.CommandType = (System.Data.CommandType)this.CommandType;
            foreach (DBParameter param in this.Parameters)
            {
                if (param.Value != null)
                    cmd.Parameters.Add(new SqlParameter(param.ParameterName, param.Value));
                else
                    cmd.Parameters.Add(new SqlParameter(param.ParameterName, DBNull.Value));
            }
    
            SqlDataAdapter sqlDA = new SqlDataAdapter(cmd);
            System.Data.DataSet dsResult = new System.Data.DataSet();
            sqlDA.Fill(dsResult);
            m_dataTable = dsResult.Tables[0];
    
            conn.Close();
            return new DBDataReader(this);
        }
    }
    View Code

    4. 小结:最后还需要实现的几个接口包括:IDataReader,IDataParameter, IDataMultiValueParameter,IDataParameterCollection,并要实现其中的某些方法。完成以上步骤后基本所有的工作已经完成,接下来就是部署DLL了。

    5. 部署:部署有两个选择,一种是供SSRS在设计模式使用DPE,另一种是供SSRS在发布后在Reporting Server上使用DPE。两种方法的发布路径及配置文件均不同,但过程都极为相似。以下是两种方式的区别:

    1. Open the project "ProjectName" and rebuild it ;
    2. Copy the file "ProjectName.dll" and "ProjectName.pdb" inside folder "ProjectNameProjectNameinDebug" to path "C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDEPrivateAssemblies";
    3. Copy file "ProjectName.config" inside project root directory to path "C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDEPrivateAssemblies";
    4. Open the file "RSReportDesigner.config" inside folder "C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDEPrivateAssemblies"(The exactly path base on the Visio Studio version you installed in your local);
       4.1. Find the node <Data> inside node <Extensions>, add node "<Extension Name="XXXX DataSource Extension" Type="ProjectName.DBConnection, ProjectName" />" inside node <Data>;
       4.2  Find the node <Desinger> inside node <Extensions>, add node "<Extension Name="XXXX DataSource Extension" Type="Microsoft.ReportingServices.QueryDesigners.GenericQueryDesigner,Microsoft.ReportingServices.QueryDesigners"/>" inside node <Desinger>;
    
    5. Open the file "RSPreviewPolicy.config" inside folder "C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDEPrivateAssemblies"(The exactly path base on the Visio Studio version you installed in your local);
       5.1  Find the node <Data> inside node <Extensions>, add node below content inside node <CodeGroup>;
        <CodeGroup
             class="UnionCodeGroup"
             version="1"
             PermissionSetName="FullTrust"
             Name="XXXX DataSource Extension"
             Description="Code group for my Custom DataSource for data processing extension">
                 <IMembershipCondition
                 class="UrlMembershipCondition"
                 version="1"
                 Url="C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDEPrivateAssembliesProjectName.dll"
                 />
        </CodeGroup>
    
    6. Open your DataSource of SSRS report ,select the Embedded connections and choice the DPE named "XXXX DataSource Extension" from the dropdownlist of Type;
    7. Click OK button to use the DPE.
    
    PS.
    1. Please close and open your Visio Studio again, if you can not find the DPE named "XXXX DataSource Extension" after you finished above steps;
    
    Using the DEP in Report Server(After deploy to Reporting Server):
    ***********************************************************************************************************
    1. Open the project "ProjectName" and rebuild it ;
    2. Copy the file "ProjectName.dll" and "ProjectName.pdb" inside folder "ProjectNameProjectNameinDebug" to path "C:Program FilesMicrosoft SQL ServerMSRS12.MSSQLSERVERReporting ServicesReportServerin";
    3. Copy file "ProjectName.config" inside project root directory to path "C:Program FilesMicrosoft SQL ServerMSRS12.MSSQLSERVERReporting ServicesReportServerin";
    4. Open the file "rsreportserver.config" inside folder "C:Program FilesMicrosoft SQL ServerMSRS12.MSSQLSERVERReporting ServicesReportServer"(The exactly path base on the Visio Studio version you installed in your local);
       4.1. Find the node <Data> inside node <Extensions>, add below content inside node <Data>;
            <Extension Name="DPEName" Type="ProjectName.DBConnection,ProjectName">
                <Configuration>
                    <connectionStrings configSource="inProjectName.config" />
                </Configuration>
            </Extension>
       4.2  Find the node <Desinger> inside node <Extensions>, add below  inside node <Desinger>;
    
    5. Open your DataSource of SSRS report in Reporting Server,then open the report through right click it and select "Manage";
    6. Click "Data Source" from the menu located left side of the new page;
    7. Select "A Custom Data Source" in the right side of the page,followed by selecting DPE named "XXXX DataSource Extension" as the Data source type;
    8. Click Apply button to use the DPE.
    
    PS.
    1. Please restart the Reporting Services if you still can not find the DataSource manage page after you finished above steps;
    View Code

    6. 总结:经过这次对DPE的使用,总体来说没有什么难度,但在使用的过程中最好能下载一个例子,然后在其基础上进行修改,这样会节约很多时间。此外还要注意的是使用的.Net Framework的版本,刚开始由于版本不正确,导致一直找不到自定义的DPE,最后经过自己重复测试才找到原因!以下是Google是找到的几个链接,在我使用的过程中帮了我很大的忙:

  • 相关阅读:
    利用python爬虫关键词批量下载高清大图
    多源最短路径算法—Floyd算法
    Dijkstra算法详细(单源最短路径算法)
    写博客没高质量配图?python爬虫教你绕过限制一键搜索下载图虫创意图片!
    并查集(不相交集合)详解与java实现
    AVL树(二叉平衡树)详解与实现
    二叉树——前序遍历、中序遍历、后序遍历、层序遍历详解(递归非递归)
    CSS基础总结
    HTML基础总结
    JavaSE 软件工程师 认证考试试卷3
  • 原文地址:https://www.cnblogs.com/sccd/p/6262562.html
Copyright © 2011-2022 走看看