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

IConnectionStringsProvider Interface

Allows you to resolve a connection string by name for the Entity Framework, XPO, and JSON data sources.

Namespace: DevExpress.Data.Entity

Assembly: DevExpress.Data.v20.2.dll

NuGet Packages: DevExpress.Data, DevExpress.WindowsDesktop.Data

Declaration

public interface IConnectionStringsProvider

Remarks

You can implement a custom IConnectionStringsProvider service and register it to translate a connection name to a connection string for the following data sources:

If the report is bound to the SqlDataSource, use the IConnectionProviderService instead of the IConnectionStringsProvider service.

When a reporting component (the Document Viewer or Report Designer) in your application loads a report, the component attempts to instantiate a data source to which a report is bound. Then the component populates the data source and retrieves data for the report. If the data source connection string is specified only by name (connection parameters are not specified), the reporting component calls a connection string provider service to obtain a connection string. Built-in services retrieve connections from the application configuration files, and you can implement an alternative IConnectionStringsProvider service to specify a connection at runtime.

Example

The following code is a custom service that returns the Entity Framework connection string by name:

using DevExpress.Data.Entity;

public class CustomConnectionStringsProvider : IConnectionStringsProvider
{
    public static ConnectionStringInfo Connection { get; private set; }
    static CustomConnectionStringsProvider()
    {
        Connection = new ConnectionStringInfo()
        {
            Name = "NorthwindEntities",
            ProviderName = "System.Data.EntityClient",
            RunTimeConnectionString =
            "metadata=res://*/Model1.csdl|" +
            "res://*/Model1.ssdl|" +
            "res://*/Model1.msl;" +
            "provider=System.Data.SqlClient;" +
            "provider connection string=\"data source = localhost;" +
            " initial catalog = Northwind; integrated security = True;" +
            " multipleactiveresultsets = True; app = EntityFramework\""
        };
    }
    private readonly IConnectionStringInfo[] connections;
    public CustomConnectionStringsProvider()
    {
        connections = new IConnectionStringInfo[] { Connection };
    }
    public IConnectionStringInfo[] GetConnections() { return connections; }
    public IConnectionStringInfo[] GetConfigFileConnections() { return connections; }
    public IConnectionStringInfo GetConnectionStringInfo(string connectionStringName) {
        return connections[0];
    }
    public string GetConnectionString(string connectionStringName)
    {
        return connections[0].RunTimeConnectionString;
    }
}

You should register the service before the component loads a report, as shown in the following code:

  • Document Viewer

    using DevExpress.Data.Entity;
    using DevExpress.XtraReports.UI;
    using System;
    // ...
    var reportPrintTool = new ReportPrintTool(new XtraReport1());
    reportPrintTool.PrintingSystem.AddService(typeof(IConnectionStringsProvider), 
        new CustomConnectionStringsProvider());
    reportPrintTool.ShowRibbonPreviewDialog();
    
  • Report Designer

    using DevExpress.Data.Entity;
    using DevExpress.XtraReports.UI;
    using System;
    // ...
    ReportDesignTool designer = new ReportDesignTool(new XtraReport());
    // Does not allow end users to create new connections.
    designer.DesignRibbonForm.DesignMdiController.DataSourceWizardSettings.
        SqlWizardSettings.DisableNewConnections = true;
    
    designer.DesignRibbonForm.DesignMdiController.AddService(typeof(IConnectionStringsProvider), 
        new CustomConnectionStringsProvider());
    designer.ShowRibbonDesignerDialog();
    

In the Report Designer, the IConnectionStringsProvider service populates the list of available Entity Framework connections. The code above allows users to select only EF connections supplied by the IConnectionStringsProvider service when they create new report data sources. The following image demonstrates that the user is restricted to the list of available connections:

IConnectionStringsProvider Entity Framework Available Connections

See Also