Skip to main content
A newer version of this page is available. .

IConnectionProviderService Interface

Allows you to resolve a data connection by name when the SqlDataSource component retrieves data from an external data source.

Namespace: DevExpress.DataAccess.Wizard.Services

Assembly: DevExpress.DataAccess.v21.1.dll

NuGet Packages: DevExpress.DataAccess, DevExpress.Win.Design

Declaration

public interface IConnectionProviderService

Remarks

Implement a class with the IConnectionProviderService interface and register it as a service to restore the data connection when the SqlDataSource attempts to retrieve data from the data source.

If the data connection is specified with parameters (that is, the SqlDataSource.ConnectionParameters property is not null), the SqlDataSource component raises the ConfigureDataConnection event and does not call the IConnectionProviderService.

The following code resolves any connection name to a local Microsoft SQL Server database connection:

using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.Wizard.Services;
using DevExpress.XtraReports.UI;
using System;
using System.ComponentModel.Design;
// ...
public class CustomConnectionProviderService : IConnectionProviderService
{
    public SqlDataConnection LoadConnection(string connectionName)
    {
        MsSqlConnectionParameters connectionParameters = new MsSqlConnectionParameters()
        {
            ServerName = "localhost",
            DatabaseName = "NorthWind",
            UserName = null,
            Password = null,
            AuthorizationType = MsSqlAuthorizationType.Windows
        };
        return new SqlDataConnection(connectionName, connectionParameters);
    }
}

Use in Reporting

The service calls the IConnectionProviderService.LoadConnection method for each connection serialized in the report definition file, and returns the SqlDataConnection object.

You can use the service in an application that invokes the Print Preview or End-User Report Designer windows:

WinForms

The following code adds a custom connection provider service to the report’s PrintingSystem and displays the Print Preview:

using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.Wizard.Services;
using DevExpress.XtraReports.UI;
using System;
using System.ComponentModel.Design;
// ...
XtraReport report = new XtraReport();
report.LoadLayout("XtraReport1.repx");
(report as IServiceContainer).AddService(typeof(IConnectionProviderService), 
    new CustomConnectionProviderService());
ReportPrintTool previewTool = new ReportPrintTool(report);
previewTool.ShowPreviewDialog();

The following code registers the service in the End-User Report Designer’s DesignPanelLoaded event handler and launches the End-User Report Designer:

using DevExpress.DataAccess.ConnectionParameters;
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.Wizard.Services;
using DevExpress.XtraReports.UI;
using System;
using System.ComponentModel.Design;
// ...
public void ShowDesigner()
{
    XtraReport report = new XtraReport();
    report.LoadLayout("XtraReport1.repx");
    ReportDesignTool designTool = new ReportDesignTool(report);
    designTool.DesignRibbonForm.DesignMdiController.DesignPanelLoaded += 
        DesignMdiController_DesignPanelLoaded;
    designTool.ShowRibbonDesignerDialog();

    void ReplaceService(IServiceContainer container, 
        Type serviceType, 
        object serviceInstance)
    {
        if (container.GetService(serviceType) != null)
            container.RemoveService(serviceType);
        container.AddService(serviceType, serviceInstance);
    }
    void DesignMdiController_DesignPanelLoaded(object sender, 
        DevExpress.XtraReports.UserDesigner.DesignerLoadedEventArgs e)
    {
        ReplaceService(e.DesignerHost, typeof(IConnectionProviderService), 
            new CustomConnectionProviderService());
    }
}

WPF

The following code adds a custom connection provider service to the report’s PrintingSystem and displays the Document Preview:

using DevExpress.DataAccess.Wizard.Services;
using DevExpress.XtraReports.UI;

// ...

XtraReport report = new XtraReport();
report.LoadLayout("XtraReport1.repx");
report.PrintingSystem.AddService(typeof(IConnectionProviderService), new CustomConnectionProviderService());  
documentPreviewControl.DocumentSource = report;
report.CreateDocument();

The following code registers the service and launches the End-User Report Designer:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dxrud="http://schemas.devexpress.com/winfx/2008/xaml/reports/userdesigner" x:Class="TestDesigner.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <dxrud:ReportDesigner Name="ReportDesigner">
        <dxrud:ReportDesigner.ServicesRegistry>
            <dxda:TypeEntry ServiceType="{x:Type dxdaw:IConnectionStorageService}" ConcreteType="{x:Type local:ConnectionStorageService}" />
            <dxda:TypeEntry ServiceType="{x:Type dxdaw:IConnectionProviderService}" ConcreteType="{x:Type local:ConnectionProviderService}" />
        </dxrud:ReportDesigner.ServicesRegistry>
    </dxrud:ReportDesigner>
</Window>

Web Report Viewer and Designer

Review the following help topic for more information and code samples: IDataSourceWizardConnectionStringsProvider.

See Also