Manage Connection at Runtime
- 3 minutes to read
Invoke the Connection Editor
Use the SqlDataSourceUIHelper.ConfigureConnection and SqlDataSourceUIHelper.ConfigureConnectionParameters methods to invoke the Connection Editor wizard at runtime. The ConfigureConnection and ConfigureConnectionParameters methods are available as the SqlDataSource class extension methods if the project references the DevExpress.DataAccess.v24.1.UI.dll and the DevExpress.DataAccess.UI.Sql namespace is explicitly imported with a using
directive:
using DevExpress.DataAccess.UI.Sql;
You can specify the ConfigureConnectionContext instance as the method parameter to customize the Connection Editor.
Restrict the List of Available Data Providers
The following code customizes the list of available data providers. It removes all data providers except the MSSqlServer.
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.UI.Sql;
SqlDataSource sqlDS = new SqlDataSource();
ConfigureConnectionContext config = new ConfigureConnectionContext();
config.DataProviders.RemoveAll(x => x.ProviderKey != "MSSqlServer");
sqlDS.ConfigureConnectionParameters(config);
Disable New Connections
The following code restricts the user to select a connection from the list.
using DevExpress.DataAccess.Sql;
using DevExpress.DataAccess.UI.Sql;
SqlDataSource sqlDS = new SqlDataSource();
ConfigureConnectionContext config = new ConfigureConnectionContext();
config.Options = DevExpress.DataAccess.Wizard.SqlWizardOptions.DisableNewConnections;
sqlDataSource1.ConfigureConnection(config);
Application Configuration File
You can define different connection strings in the application configuration file and use the connection name to specify the SqlDataSource.ConnectionName property when the SqlDataSource is initialized.
Customize Connection Parameters
The SqlDataSource.ConfigureDataConnection event occurs if the SqlDataSource.ConnectionParameters property is specified. The event is not raised if the data connection is stored by name, without parameters. The event is raised each time the SqlDataSource connects to a database. You can handle this event to change the parameters required to establish a data connection (server name, database name, user credentials, data file path).
The ConfigureDataConnection event handler allows you to use the e.ConnectionName property to identify the name of the current connection and e.ConnectionParameters property to access and modify connection parameters. You cannot modify the SqlDataSource.ConnectionName
property in the event handler.
Reporting - Manage Connections at Runtime
Handle the DataSourceDemanded Event
The DataSourceDemanded event occurs before a report generates a document, and allows you to specify the report’s data source.
Use the IConnectionProviderService
If the application loads a report from a .REPX file (deserializes the report), you can use a custom service to resolve the connection name stored in the report layout file. Add a custom service that implements the IConnectionProviderService interface to the report instance (the report is cast to the IServiceContainer interface). The report calls the IConnectionProviderService.LoadConnection method for each data connection stored in a REPX file, if the connection is stored by name, without connection parameters.
More Information
- ASP.NET Web Forms: Update Database Connections (Web Forms)
- ASP.NET MVC: Update Database Connections (ASP.NET MVC)
- ASP.NET Core: SQL Database - Update Connections (ASP.NET Core)
Dashboards - Manage Connections at Runtime
Handle the Dashboard.ConfigureDataConnection event that occurs before the dashboard populates its data source.