Skip to main content

Use a Custom Database Schema

  • 5 minutes to read

This topic describes the next step after you have registered data connection strings for Web Report Designer and the corresponding data sources are available to users.

You must set up a custom database schema provider to control which tables, views, and stored procedures are available in the Data Source Wizard based on certain conditions. The Data Source Wizard in the Web Report Designer uses it to determine which data objects are available to a user. The Query Builder automatically uses a custom database schema provider registered in the application.

A custom database schema provider allows you to restrict access to tables, views, stored procedures, and data fields in the Web Report Designer, Report Designer Preview, and in the Query Builder. Fewer objects improve the operation speed when a report rebuilds the database schema before the data is displayed.

Custom Database Schema Provider Implementation

Implement the IDBSchemaProviderEx interface to create a custom database schema provider (named MyDBSchemaProvider).

using DevExpress.DataAccess.Sql;
using DevExpress.Xpo.DB;
using System.Linq;
// ...

public class MyDBSchemaProvider : IDBSchemaProviderEx {
    DBSchemaProviderEx provider;
    public MyDBSchemaProvider() {
        this.provider = new DBSchemaProviderEx();
    }

    public DBTable[] GetTables(SqlDataConnection connection, params string[] tableList) {
        return provider.GetTables(connection, tableList)
            .Where(table => table.Name.StartsWith("C"))
            .ToArray();
    }

    public DBTable[] GetViews(SqlDataConnection connection, params string[] viewList) {
        return provider.GetViews(connection, viewList)
            .Where(view => view.Name.StartsWith("Order"))
            .ToArray();
    }

    public DBStoredProcedure[] GetProcedures(SqlDataConnection connection, params string[] procedureList) {
        return provider.GetProcedures(connection, procedureList)
            .Where(storedProcedure => storedProcedure.Arguments.Count == 0)
            .ToArray();
    }

    public void LoadColumns(SqlDataConnection connection, params DBTable[] tables) {
        provider.LoadColumns(connection, tables);
    }
}

Database Schema Provider Factory

Data Source Wizard and Query Builder

Data Source Wizard and Query Builder components use a factory that implement the IDataSourceWizardDBSchemaProviderExFactory interface. The following code example is a factory that returns a new custom data store schema provider MyDBSchemaProvider class in the IDataSourceWizardDBSchemaProviderExFactory.Create method:

using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.Web;
// ...

public class MyDataSourceWizardDBSchemaProviderFactory : IDataSourceWizardDBSchemaProviderExFactory {
    public IDBSchemaProviderEx Create() {
        return new MyDBSchemaProvider();
    }
}

To register a custom database schema provider factory in the application, call the static DefaultReportDesignerContainer.RegisterDataSourceWizardDBSchemaProviderExFactory<T> method at application startup:

public class Global_asax : System.Web.HttpApplication {
    void Application_Start(object sender, EventArgs e) {
        // ...                          
        // Register a connection string provider.
        // ...
        // Register the custom provider factory.
        DevExpress.XtraReports.Web.ReportDesigner.DefaultReportDesignerContainer
        .RegisterDataSourceWizardDBSchemaProviderExFactory<MyDataSourceWizardDBSchemaProviderFactory>();
    }
    // ...
}

Report Designer Preview

A custom database schema provider registered for Report Designer Preview allows the preview component to retrieve data only for tables, views, stored procedures, and data fields specified in the custom database schema provider.

Use Same Factory As Report Designer

Report Designer Preview component can use a factory implemented in the previous step without modifications. For this, call the DefaultReportDesignerContainer.RegisterDataSourceWizardDBSchemaProviderExFactory<T>(Boolean) method at application startup with the overrideReportPreviewProviderparameter set to true:

using DevExpress.DataAccess.Web;
using System;
using DevExpress.XtraReports.Web.ReportDesigner
// ...

public class Global_asax : System.Web.HttpApplication {
    void Application_Start(object sender, EventArgs e) {
        // ...                          
        // Register a connection string provider.
        // ...
        // Register the custom provider factory.
        evExpress.XtraReports.Web.ReportDesigner.DefaultReportDesignerContainer
            .RegisterDataSourceWizardDBSchemaProviderExFactory<MyDataSourceWizardDBSchemaProviderFactory>(true);
    }
    // ...
}

Use Different Factory

The DefaultReportDesignerContainer.RegisterDataSourceWizardDBSchemaProviderExFactory<T>(Boolean) method with the overrideReportPreviewProviderparameter set to true instructs Report Designer to use the same database schema factory for Preview and Query Bulder.

If you want to use a different database schema factory for Report Preview, implement a class with the IDBSchemaProviderExFactory interface and create a new custom schema provider in the IDBSchemaProviderExFactory.Create method.

To register a custom database schema provider factory for the Report Preview, call the static DefaultWebDocumentViewerContainer.RegisterDBSchemaProviderExFactory<T> method at application startup:

public class Global_asax : System.Web.HttpApplication {
    void Application_Start(object sender, EventArgs e) {
        // ...                          
        // Register a connection string provider.
        // ...
        // Register the custom provider factory.
        DevExpress.XtraReports.Web.WebDocumentViewer.DefaultWebDocumentViewerContainer.
        RegisterDBSchemaProviderExFactory<MyDataSourceWizardDBSchemaProviderFactory>();

    }
    // ...
}
See Also